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.api; |
27 | |
|
28 | |
import com.sun.javafx.api.*; |
29 | |
import java.io.InputStream; |
30 | |
import java.io.OutputStream; |
31 | |
import java.io.PrintWriter; |
32 | |
import java.util.Collections; |
33 | |
import java.util.EnumSet; |
34 | |
import java.util.Set; |
35 | |
import javax.lang.model.SourceVersion; |
36 | |
import javax.tools.*; |
37 | |
|
38 | |
import com.sun.tools.javac.util.Context; |
39 | |
import com.sun.tools.javac.util.JavacFileManager; |
40 | |
import com.sun.tools.javac.util.Log; |
41 | |
import com.sun.tools.javac.util.Options; |
42 | |
import com.sun.tools.javafx.main.JavafxOption; |
43 | |
import com.sun.tools.javafx.main.Main; |
44 | |
import com.sun.tools.javafx.main.RecognizedOptions.GrumpyHelper; |
45 | |
import com.sun.tools.javafx.main.RecognizedOptions; |
46 | |
import com.sun.tools.javafx.util.JavafxFileManager; |
47 | |
import com.sun.tools.javafx.util.MsgSym; |
48 | |
import java.io.Writer; |
49 | |
import java.nio.charset.Charset; |
50 | |
import java.util.Iterator; |
51 | |
import java.util.Locale; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | 56 | public final class JavafxcTool implements JavafxCompiler { |
60 | 56 | private final Context dummyContext = new Context(); |
61 | |
|
62 | 56 | private final PrintWriter silent = new PrintWriter(new OutputStream(){ |
63 | |
@Override |
64 | 0 | public void write(int b) {} |
65 | |
}); |
66 | |
|
67 | 56 | private final Main sharedCompiler = new Main("javafxc", silent); |
68 | |
{ |
69 | 56 | sharedCompiler.setOptions(Options.instance(dummyContext)); |
70 | 56 | } |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public static JavafxcTool create() { |
77 | 41 | return new JavafxcTool(); |
78 | |
} |
79 | |
|
80 | |
@Override |
81 | |
public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) { |
82 | 351 | if (err == null) |
83 | 0 | err = System.err; |
84 | 2818 | for (String argument : arguments) |
85 | 2467 | argument.getClass(); |
86 | 351 | return com.sun.tools.javafx.Main.compile(arguments, new PrintWriter(err, true)); |
87 | |
} |
88 | |
|
89 | |
@Override |
90 | |
public Set<SourceVersion> getSourceVersions() { |
91 | 1 | return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3, |
92 | |
SourceVersion.latest())); |
93 | |
} |
94 | |
|
95 | |
@Override |
96 | |
public JavacFileManager getStandardFileManager( |
97 | |
DiagnosticListener<? super JavaFileObject> diagnosticListener, |
98 | |
Locale locale, |
99 | |
Charset charset) { |
100 | 44 | Context context = new Context(); |
101 | 44 | if (diagnosticListener != null) |
102 | 35 | context.put(DiagnosticListener.class, diagnosticListener); |
103 | 44 | context.put(Log.outKey, new PrintWriter(System.err, true)); |
104 | 44 | return new JavafxFileManager(context, true, charset); |
105 | |
} |
106 | |
|
107 | |
@Override |
108 | |
public JavafxcTask getTask(Writer out, |
109 | |
JavaFileManager fileManager, |
110 | |
DiagnosticListener<? super JavaFileObject> diagnosticListener, |
111 | |
Iterable<String> options, |
112 | |
Iterable<? extends JavaFileObject> compilationUnits) |
113 | |
{ |
114 | 52 | final String kindMsg = "All compilation units must be of SOURCE kind"; |
115 | 52 | if (options != null) |
116 | 29 | for (String option : options) |
117 | 232 | option.getClass(); |
118 | 52 | if (compilationUnits != null) { |
119 | 44 | for (JavaFileObject cu : compilationUnits) { |
120 | 44 | if (cu.getKind() != JavaFileObject.Kind.SOURCE) |
121 | 0 | throw new IllegalArgumentException(kindMsg); |
122 | |
} |
123 | |
} |
124 | |
|
125 | 52 | Context context = new Context(); |
126 | |
|
127 | 52 | if (diagnosticListener != null) |
128 | 44 | context.put(DiagnosticListener.class, diagnosticListener); |
129 | |
|
130 | 52 | if (out == null) |
131 | 23 | context.put(Log.outKey, new PrintWriter(System.err, true)); |
132 | |
else |
133 | 29 | context.put(Log.outKey, new PrintWriter(out, true)); |
134 | |
|
135 | 52 | if (fileManager == null) |
136 | 8 | fileManager = getStandardFileManager(diagnosticListener, null, null); |
137 | 52 | context.put(JavaFileManager.class, fileManager); |
138 | 52 | processOptions(context, fileManager, options); |
139 | 52 | Main compiler = new Main("javacTask", context.get(Log.outKey)); |
140 | 52 | return new JavafxcTaskImpl(this, compiler, options, context, compilationUnits); |
141 | |
} |
142 | |
|
143 | |
private static void processOptions(Context context, |
144 | |
JavaFileManager fileManager, |
145 | |
Iterable<String> options) |
146 | |
{ |
147 | 52 | if (options == null) |
148 | 23 | return; |
149 | |
|
150 | 29 | Options optionTable = Options.instance(context); |
151 | |
|
152 | 29 | JavafxOption[] recognizedOptions = |
153 | |
RecognizedOptions.getJavacToolOptions(new GrumpyHelper()); |
154 | 29 | Iterator<String> flags = options.iterator(); |
155 | 203 | while (flags.hasNext()) { |
156 | 174 | String flag = flags.next(); |
157 | |
int j; |
158 | 2436 | for (j=0; j<recognizedOptions.length; j++) |
159 | 2407 | if (recognizedOptions[j].matches(flag)) |
160 | 145 | break; |
161 | |
|
162 | 174 | if (j == recognizedOptions.length) { |
163 | 29 | if (fileManager.handleOption(flag, flags)) { |
164 | 29 | continue; |
165 | |
} else { |
166 | 0 | String msg = Main.getLocalizedString(MsgSym.MESSAGE_ERR_INVALID_FLAG, flag); |
167 | 0 | throw new IllegalArgumentException(msg); |
168 | |
} |
169 | |
} |
170 | |
|
171 | 145 | JavafxOption option = recognizedOptions[j]; |
172 | 145 | if (option.hasArg()) { |
173 | 29 | if (!flags.hasNext()) { |
174 | 0 | String msg = Main.getLocalizedString(MsgSym.MESSAGE_ERR_REQ_ARG, flag); |
175 | 0 | throw new IllegalArgumentException(msg); |
176 | |
} |
177 | 29 | String operand = flags.next(); |
178 | 29 | if (option.process(optionTable, flag, operand)) |
179 | |
|
180 | |
|
181 | 0 | throw new IllegalArgumentException(flag + " " + operand); |
182 | 29 | } else { |
183 | 116 | if (option.process(optionTable, flag)) |
184 | |
|
185 | |
|
186 | 0 | throw new IllegalArgumentException(flag); |
187 | |
} |
188 | 145 | } |
189 | 29 | } |
190 | |
|
191 | |
@Override |
192 | |
public int isSupportedOption(String option) { |
193 | 4 | JavafxOption[] recognizedOptions = |
194 | |
RecognizedOptions.getJavacToolOptions(new GrumpyHelper()); |
195 | 76 | for (JavafxOption o : recognizedOptions) { |
196 | 74 | if (o.matches(option)) |
197 | 2 | return o.hasArg() ? 1 : 0; |
198 | |
} |
199 | 2 | return -1; |
200 | |
} |
201 | |
} |