• R/O
  • SSH
  • HTTPS

nina: Commit


Commit MetaInfo

Revision682 (tree)
Time2018-12-01 13:54:23
Authory-moriguchi

Log Message

compile and create JAR files

Change Summary

Incremental Difference

--- nina/java/translator/net/morilib/nina/cmd/NinaInfo.java (revision 681)
+++ nina/java/translator/net/morilib/nina/cmd/NinaInfo.java (revision 682)
@@ -15,6 +15,7 @@
1515 */
1616 package net.morilib.nina.cmd;
1717
18+import java.io.File;
1819 import java.io.IOException;
1920 import java.io.InputStream;
2021 import java.io.OutputStream;
@@ -60,13 +61,13 @@
6061 * @param s
6162 * @return
6263 */
63- public Object getTitleFile(String s);
64+ public File getTitleFile(String s);
6465
6566 /**
6667 *
6768 * @return
6869 */
69- public Object getOutputDir();
70+ public File getOutputDir();
7071
7172 /**
7273 *
@@ -135,4 +136,11 @@
135136 */
136137 public ShFile getShOutputFile(ShFileSystem fs, String p, String f);
137138
139+ /**
140+ *
141+ * @param key
142+ * @return
143+ */
144+ public String getOption(String key);
145+
138146 }
--- nina/java/translator/net/morilib/nina/cmd/Ninac.java (revision 681)
+++ nina/java/translator/net/morilib/nina/cmd/Ninac.java (revision 682)
@@ -67,6 +67,7 @@
6767 } else {
6868 tr.translate(s, rd, opts, opts.getLibraryList(), prms,
6969 pr);
70+ tr.postTranslate();
7071 }
7172
7273 if(opts.getReport() != null && opts.getReport() != tr) {
--- nina/java/translator/net/morilib/nina/cmd/NinatOptions.java (revision 681)
+++ nina/java/translator/net/morilib/nina/cmd/NinatOptions.java (revision 682)
@@ -102,6 +102,10 @@
102102 console = this;
103103 } else if(s.equals("--xml")) {
104104 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");
105109 } else if((m = TABP.matcher(s)).matches()) {
106110 Quadro.setTab(Integer.valueOf(m.group(1)));
107111 } else if((x = s.indexOf('=')) < 0) {
@@ -231,7 +235,7 @@
231235 * @see net.morilib.nina.cmd.NinaInfo#getOutputDir()
232236 */
233237 @Override
234- public Object getOutputDir() {
238+ public File getOutputDir() {
235239 String s;
236240
237241 return new File((s = getOption("output")).equals("") ?
@@ -241,7 +245,7 @@
241245 /* (non-Javadoc)
242246 * @see net.morilib.nina.cmd.NinaInfo#getTitleFile(java.lang.String)
243247 */
244- public Object getTitleFile(String t) {
248+ public File getTitleFile(String t) {
245249 String s;
246250 char[] a;
247251
@@ -330,10 +334,8 @@
330334 new File((File)getOutputDir(), f));
331335 }
332336
333- /**
334- *
335- * @param key
336- * @return
337+ /* (non-Javadoc)
338+ * @see net.morilib.nina.cmd.NinaInfo#getOption(java.lang.String)
337339 */
338340 public String getOption(String key) {
339341 String s;
--- nina/java/translator/net/morilib/nina/interpret/NinaScript.java (revision 681)
+++ nina/java/translator/net/morilib/nina/interpret/NinaScript.java (revision 682)
@@ -96,12 +96,12 @@
9696 }
9797
9898 @Override
99- public String getTitleFile(String s) {
99+ public File getTitleFile(String s) {
100100 return null;
101101 }
102102
103103 @Override
104- public Object getOutputDir() {
104+ public File getOutputDir() {
105105 return new File(".");
106106 }
107107
@@ -156,6 +156,11 @@
156156 return new FileInputStream((File)file);
157157 }
158158
159+ @Override
160+ public String getOption(String key) {
161+ return "";
162+ }
163+
159164 };
160165
161166 //
--- nina/java/translator/net/morilib/nina/translate/AbstractNinaTranslator.java (revision 681)
+++ nina/java/translator/net/morilib/nina/translate/AbstractNinaTranslator.java (revision 682)
@@ -1229,4 +1229,10 @@
12291229 opts.getConsole(), opts);
12301230 }
12311231
1232+ /* (non-Javadoc)
1233+ * @see net.morilib.nina.translate.NinaTranslator#postTranslate()
1234+ */
1235+ public void postTranslate() throws IOException {
1236+ }
1237+
12321238 }
--- nina/java/translator/net/morilib/nina/translate/NinaTranslator.java (revision 681)
+++ nina/java/translator/net/morilib/nina/translate/NinaTranslator.java (revision 682)
@@ -60,6 +60,12 @@
6060
6161 /**
6262 *
63+ * @throws IOException
64+ */
65+ public void postTranslate() throws IOException;
66+
67+ /**
68+ *
6369 * @param std
6470 */
6571 public void reportStatistics(PrintStream std);
--- nina/java/translator/net/morilib/nina/translate/NinaTranslatorJava.java (revision 681)
+++ nina/java/translator/net/morilib/nina/translate/NinaTranslatorJava.java (revision 682)
@@ -15,6 +15,11 @@
1515 */
1616 package net.morilib.nina.translate;
1717
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;
1823 import java.io.IOException;
1924 import java.io.InputStream;
2025 import java.io.PrintStream;
@@ -23,9 +28,14 @@
2328 import java.util.List;
2429 import java.util.Map;
2530 import java.util.Set;
31+import java.util.jar.JarEntry;
32+import java.util.jar.JarOutputStream;
2633 import java.util.regex.Matcher;
2734 import java.util.regex.Pattern;
2835
36+import javax.tools.JavaCompiler;
37+import javax.tools.ToolProvider;
38+
2939 import net.morilib.automata.DFAState;
3040 import net.morilib.nina.DFABuilder;
3141 import net.morilib.nina.NinaState;
@@ -1194,4 +1204,90 @@
11941204 env.bind("VALTYPE", q.getOptionNvl("valtype", "Object"));
11951205 }
11961206
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+
11971293 }
--- nina/java/translator/net/morilib/nina/translate/ProxyNinaTranslator.java (revision 681)
+++ nina/java/translator/net/morilib/nina/translate/ProxyNinaTranslator.java (revision 682)
@@ -95,6 +95,13 @@
9595 }
9696
9797 /* (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)
98105 * @see net.morilib.nina.translate.NinaTranslator#getExtension()
99106 */
100107 @Override
Show on old repository browser