Revisión | 682 (tree) |
---|---|
Tiempo | 2018-12-01 13:54:23 |
Autor | y-moriguchi |
compile and create JAR files
@@ -15,6 +15,7 @@ | ||
15 | 15 | */ |
16 | 16 | package net.morilib.nina.cmd; |
17 | 17 | |
18 | +import java.io.File; | |
18 | 19 | import java.io.IOException; |
19 | 20 | import java.io.InputStream; |
20 | 21 | import java.io.OutputStream; |
@@ -60,13 +61,13 @@ | ||
60 | 61 | * @param s |
61 | 62 | * @return |
62 | 63 | */ |
63 | - public Object getTitleFile(String s); | |
64 | + public File getTitleFile(String s); | |
64 | 65 | |
65 | 66 | /** |
66 | 67 | * |
67 | 68 | * @return |
68 | 69 | */ |
69 | - public Object getOutputDir(); | |
70 | + public File getOutputDir(); | |
70 | 71 | |
71 | 72 | /** |
72 | 73 | * |
@@ -135,4 +136,11 @@ | ||
135 | 136 | */ |
136 | 137 | public ShFile getShOutputFile(ShFileSystem fs, String p, String f); |
137 | 138 | |
139 | + /** | |
140 | + * | |
141 | + * @param key | |
142 | + * @return | |
143 | + */ | |
144 | + public String getOption(String key); | |
145 | + | |
138 | 146 | } |
@@ -67,6 +67,7 @@ | ||
67 | 67 | } else { |
68 | 68 | tr.translate(s, rd, opts, opts.getLibraryList(), prms, |
69 | 69 | pr); |
70 | + tr.postTranslate(); | |
70 | 71 | } |
71 | 72 | |
72 | 73 | if(opts.getReport() != null && opts.getReport() != tr) { |
@@ -102,6 +102,10 @@ | ||
102 | 102 | console = this; |
103 | 103 | } else if(s.equals("--xml")) { |
104 | 104 | report = new NinaTranslatorXML(); |
105 | + } else if(s.equals("--compileJava")) { | |
106 | + opts.put("compileJava", "on"); | |
107 | + } else if(s.equals("--createJar")) { | |
108 | + opts.put("createJar", "on"); | |
105 | 109 | } else if((m = TABP.matcher(s)).matches()) { |
106 | 110 | Quadro.setTab(Integer.valueOf(m.group(1))); |
107 | 111 | } else if((x = s.indexOf('=')) < 0) { |
@@ -231,7 +235,7 @@ | ||
231 | 235 | * @see net.morilib.nina.cmd.NinaInfo#getOutputDir() |
232 | 236 | */ |
233 | 237 | @Override |
234 | - public Object getOutputDir() { | |
238 | + public File getOutputDir() { | |
235 | 239 | String s; |
236 | 240 | |
237 | 241 | return new File((s = getOption("output")).equals("") ? |
@@ -241,7 +245,7 @@ | ||
241 | 245 | /* (non-Javadoc) |
242 | 246 | * @see net.morilib.nina.cmd.NinaInfo#getTitleFile(java.lang.String) |
243 | 247 | */ |
244 | - public Object getTitleFile(String t) { | |
248 | + public File getTitleFile(String t) { | |
245 | 249 | String s; |
246 | 250 | char[] a; |
247 | 251 |
@@ -330,10 +334,8 @@ | ||
330 | 334 | new File((File)getOutputDir(), f)); |
331 | 335 | } |
332 | 336 | |
333 | - /** | |
334 | - * | |
335 | - * @param key | |
336 | - * @return | |
337 | + /* (non-Javadoc) | |
338 | + * @see net.morilib.nina.cmd.NinaInfo#getOption(java.lang.String) | |
337 | 339 | */ |
338 | 340 | public String getOption(String key) { |
339 | 341 | String s; |
@@ -96,12 +96,12 @@ | ||
96 | 96 | } |
97 | 97 | |
98 | 98 | @Override |
99 | - public String getTitleFile(String s) { | |
99 | + public File getTitleFile(String s) { | |
100 | 100 | return null; |
101 | 101 | } |
102 | 102 | |
103 | 103 | @Override |
104 | - public Object getOutputDir() { | |
104 | + public File getOutputDir() { | |
105 | 105 | return new File("."); |
106 | 106 | } |
107 | 107 |
@@ -156,6 +156,11 @@ | ||
156 | 156 | return new FileInputStream((File)file); |
157 | 157 | } |
158 | 158 | |
159 | + @Override | |
160 | + public String getOption(String key) { | |
161 | + return ""; | |
162 | + } | |
163 | + | |
159 | 164 | }; |
160 | 165 | |
161 | 166 | // |
@@ -1229,4 +1229,10 @@ | ||
1229 | 1229 | opts.getConsole(), opts); |
1230 | 1230 | } |
1231 | 1231 | |
1232 | + /* (non-Javadoc) | |
1233 | + * @see net.morilib.nina.translate.NinaTranslator#postTranslate() | |
1234 | + */ | |
1235 | + public void postTranslate() throws IOException { | |
1236 | + } | |
1237 | + | |
1232 | 1238 | } |
@@ -60,6 +60,12 @@ | ||
60 | 60 | |
61 | 61 | /** |
62 | 62 | * |
63 | + * @throws IOException | |
64 | + */ | |
65 | + public void postTranslate() throws IOException; | |
66 | + | |
67 | + /** | |
68 | + * | |
63 | 69 | * @param std |
64 | 70 | */ |
65 | 71 | public void reportStatistics(PrintStream std); |
@@ -15,6 +15,11 @@ | ||
15 | 15 | */ |
16 | 16 | package net.morilib.nina.translate; |
17 | 17 | |
18 | +import java.io.BufferedInputStream; | |
19 | +import java.io.ByteArrayInputStream; | |
20 | +import java.io.File; | |
21 | +import java.io.FileInputStream; | |
22 | +import java.io.FileOutputStream; | |
18 | 23 | import java.io.IOException; |
19 | 24 | import java.io.InputStream; |
20 | 25 | import java.io.PrintStream; |
@@ -23,9 +28,14 @@ | ||
23 | 28 | import java.util.List; |
24 | 29 | import java.util.Map; |
25 | 30 | import java.util.Set; |
31 | +import java.util.jar.JarEntry; | |
32 | +import java.util.jar.JarOutputStream; | |
26 | 33 | import java.util.regex.Matcher; |
27 | 34 | import java.util.regex.Pattern; |
28 | 35 | |
36 | +import javax.tools.JavaCompiler; | |
37 | +import javax.tools.ToolProvider; | |
38 | + | |
29 | 39 | import net.morilib.automata.DFAState; |
30 | 40 | import net.morilib.nina.DFABuilder; |
31 | 41 | import net.morilib.nina.NinaState; |
@@ -1194,4 +1204,90 @@ | ||
1194 | 1204 | env.bind("VALTYPE", q.getOptionNvl("valtype", "Object")); |
1195 | 1205 | } |
1196 | 1206 | |
1207 | + private int compileJava() { | |
1208 | + File javaFile = new File(options.getOutputDir(), options.getOutputFilename() + ".java"); | |
1209 | + String[] args = new String[] { javaFile.getAbsolutePath() }; | |
1210 | + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); | |
1211 | + int result = compiler.run(null, null, null, args); | |
1212 | + | |
1213 | + return result; | |
1214 | + } | |
1215 | + | |
1216 | + private void putJarEntry(JarOutputStream jarStream, String entryName, | |
1217 | + InputStream ins) throws IOException { | |
1218 | + byte[] buf = new byte[4096]; | |
1219 | + JarEntry entry = new JarEntry(entryName); | |
1220 | + int length; | |
1221 | + | |
1222 | + jarStream.putNextEntry(entry); | |
1223 | + while((length = ins.read(buf, 0, buf.length)) >= 0) { | |
1224 | + jarStream.write(buf, 0, length); | |
1225 | + } | |
1226 | + } | |
1227 | + | |
1228 | + private void putJarEntry(JarOutputStream jarStream, String entryName, | |
1229 | + File file) throws IOException { | |
1230 | + InputStream ins = new BufferedInputStream(new FileInputStream(file)); | |
1231 | + | |
1232 | + try { | |
1233 | + putJarEntry(jarStream, entryName, ins); | |
1234 | + } finally { | |
1235 | + ins.close(); | |
1236 | + } | |
1237 | + } | |
1238 | + | |
1239 | + private String getManifest() { | |
1240 | + return "Main-Class: " + options.getTitleFile("").getName() + "\n"; | |
1241 | + } | |
1242 | + | |
1243 | + private boolean isClassFileToCompress(File file) { | |
1244 | + String titleFileName = options.getTitleFile("").getName(); | |
1245 | + String name = file.getName(); | |
1246 | + | |
1247 | + return name.equals(titleFileName + ".class") || | |
1248 | + (name.startsWith(titleFileName + "$") && name.endsWith(".class")); | |
1249 | + } | |
1250 | + | |
1251 | + private void createJarFile() throws IOException { | |
1252 | + File jarFile = new File(options.getOutputDir(), options.getOutputFilename() + ".jar"); | |
1253 | + File[] files = options.getOutputDir().listFiles(); | |
1254 | + JarOutputStream jarStream = new JarOutputStream(new FileOutputStream(jarFile)); | |
1255 | + String entityName; | |
1256 | + | |
1257 | + try { | |
1258 | + putJarEntry(jarStream, | |
1259 | + "META-INF/MANIFEST.MF", | |
1260 | + new ByteArrayInputStream(getManifest().getBytes())); | |
1261 | + for(File f : files) { | |
1262 | + if(isClassFileToCompress(f)) { | |
1263 | + entityName = f.getName(); | |
1264 | + putJarEntry(jarStream, entityName, f); | |
1265 | + } | |
1266 | + } | |
1267 | + } finally { | |
1268 | + jarStream.close(); | |
1269 | + } | |
1270 | + | |
1271 | + for(File f : files) { | |
1272 | + if(isClassFileToCompress(f)) { | |
1273 | + f.delete(); | |
1274 | + } | |
1275 | + } | |
1276 | + } | |
1277 | + | |
1278 | + /* (non-Javadoc) | |
1279 | + * @see net.morilib.nina.translate.NinaTranslator#postTranslate() | |
1280 | + */ | |
1281 | + public void postTranslate() throws IOException { | |
1282 | + if(!options.getOption("compileJava").equals("") || | |
1283 | + quadro.isOptionDefined("compileJava")) { | |
1284 | + compileJava(); | |
1285 | + } else if(!options.getOption("createJar").equals("") || | |
1286 | + quadro.isOptionDefined("createJar")) { | |
1287 | + if(compileJava() == 0) { | |
1288 | + createJarFile(); | |
1289 | + } | |
1290 | + } | |
1291 | + } | |
1292 | + | |
1197 | 1293 | } |
@@ -95,6 +95,13 @@ | ||
95 | 95 | } |
96 | 96 | |
97 | 97 | /* (non-Javadoc) |
98 | + * @see net.morilib.nina.translate.NinaTranslator#postTranslate() | |
99 | + */ | |
100 | + public void postTranslate() throws IOException { | |
101 | + proxy.postTranslate(); | |
102 | + } | |
103 | + | |
104 | + /* (non-Javadoc) | |
98 | 105 | * @see net.morilib.nina.translate.NinaTranslator#getExtension() |
99 | 106 | */ |
100 | 107 | @Override |