Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

825
LINES

< > BotCompany Repo | #2000358 // x12.java

New Tinybrain snippet

1  
import java.io.*;
2  
import java.lang.reflect.Method;
3  
import java.net.URL;
4  
import java.net.URLClassLoader;
5  
import java.net.URLConnection;
6  
import java.security.MessageDigest;
7  
import java.security.NoSuchAlgorithmException;
8  
import java.util.*;
9  
import java.util.regex.Matcher;
10  
import java.util.regex.Pattern;
11  
12  
/**
13  
 JavaX runner version 12 as embeddable class (non-public)
14  
15  
 Changes to v11:
16  
 -Ported to Windows
17  
18  
 */
19  
20  
class x12 {
21  
  static final String version = "JavaX 12";
22  
23  
  static boolean verbose = false, translate = false, list = false, virtualizeTranslators = true;
24  
  static boolean preferCached = false;
25  
  static List<String> mainTranslators = new ArrayList<String>();
26  
  private static Map<Long, String> memSnippetCache = new HashMap<Long, String>();
27  
  private static int processesStarted;
28  
29  
  public static void main(String[] args) throws Exception {
30  
    File ioBaseDir = new File("."), inputDir = null, outputDir = null;
31  
    String src = ".";
32  
    for (int i = 0; i < args.length; i++) {
33  
      String arg = args[i];
34  
      if (arg.equals("-v") || arg.equals("-verbose"))
35  
        verbose = true;
36  
      else if (arg.equals("-finderror"))
37  
        verbose = true;
38  
      else if (arg.equals("-offline") || arg.equalsIgnoreCase("-prefercached"))
39  
        preferCached = true;
40  
      else if (arg.equals("-novirt"))
41  
        virtualizeTranslators = false;
42  
      else if (arg.equals("translate"))
43  
        translate = true;
44  
      else if (arg.equals("list"))
45  
        list = true;
46  
      else if (arg.equals("run")) {
47  
        // it's the default command anyway
48  
      } else if (arg.startsWith("input="))
49  
        inputDir = new File(arg.substring(6));
50  
      else if (arg.startsWith("output="))
51  
        outputDir = new File(arg.substring(7));
52  
      else if (arg.equals("with"))
53  
        mainTranslators.add(args[++i]);
54  
      else
55  
        src = arg;
56  
    }
57  
58  
    if (virtualizeTranslators && !preferCached)
59  
      initDiskSnippetCache(TempDirMaker_make());
60  
61  
    if (inputDir != null) {
62  
      ioBaseDir = TempDirMaker_make();
63  
      System.out.println("Taking input from: " + inputDir.getAbsolutePath());
64  
      System.out.println("Output is in: " + new File(ioBaseDir, "output").getAbsolutePath());
65  
      copyInput(inputDir, new File(ioBaseDir, "input"));
66  
    }
67  
68  
    javax4(src, ioBaseDir, translate, list);
69  
70  
    if (outputDir != null) {
71  
      copyInput(new File(ioBaseDir, "output"), outputDir);
72  
      System.out.println("Output copied to: " + outputDir.getAbsolutePath());
73  
    }
74  
75  
    if (verbose)
76  
      System.out.println("Processes started: " + processesStarted);
77  
  }
78  
79  
  public static void javax4(String src, File ioDir, boolean translate, boolean list) throws Exception {
80  
    File srcDir;
81  
    if (isSnippetID(src))
82  
      srcDir = loadSnippetAsMainJava(src);
83  
    else {
84  
      srcDir = new File(src);
85  
      if (!new File(srcDir, "main.java").exists()) {
86  
        System.out.println("This is " + version  + ".\n" +
87  
          "No main.java found, exiting");
88  
        return;
89  
      }
90  
    }
91  
92  
    // translate
93  
94  
    List<File> libraries = new ArrayList<File>();
95  
    File X = srcDir;
96  
    X = applyTranslators(X, mainTranslators, libraries);
97  
    X = defaultTranslate(X, libraries);
98  
99  
    // list or run
100  
101  
    if (translate)
102  
      System.out.println("Program translated to: " + X.getAbsolutePath());
103  
    else if (list)
104  
      System.out.println(loadTextFile(new File(X, "main.java").getPath(), null));
105  
    else
106  
      javax2(X, ioDir, false, false, libraries);
107  
  }
108  
109  
  private static File defaultTranslate(File x, List<File> libraries_out) throws Exception {
110  
    x = luaPrintToJavaPrint(x);
111  
    x = autoTranslate(x, libraries_out);
112  
    return x;
113  
  }
114  
115  
  private static File autoTranslate(File x, List<File> libraries_out) throws Exception {
116  
    String main = loadTextFile(new File(x, "main.java").getPath(), null);
117  
    List<String> lines = toLines(main);
118  
    List<String> translators = findTranslators(lines);
119  
    if (translators.isEmpty())
120  
      return x;
121  
122  
    main = fromLines(lines);
123  
    File newDir = TempDirMaker_make();
124  
    saveTextFile(new File(newDir, "main.java").getPath(), main);
125  
    return applyTranslators(newDir, translators, libraries_out);
126  
  }
127  
128  
  private static List<String> findTranslators(List<String> lines) {
129  
    List<String> translators = new ArrayList<String>();
130  
    Pattern pattern = Pattern.compile("^!([0-9# \t]+)");
131  
    for (ListIterator<String> iterator = lines.listIterator(); iterator.hasNext(); ) {
132  
      String line = iterator.next();
133  
      line = line.trim();
134  
      Matcher matcher = pattern.matcher(line);
135  
      if (matcher.find()) {
136  
        translators.addAll(Arrays.asList(matcher.group(1).split("[ \t]+")));
137  
        iterator.remove();
138  
      }
139  
    }
140  
    return translators;
141  
  }
142  
143  
  public static List<String> toLines(String s) {
144  
    List<String> lines = new ArrayList<String>();
145  
    int start = 0;
146  
    while (true) {
147  
      int i = toLines_nextLineBreak(s, start);
148  
      if (i < 0) {
149  
        if (s.length() > start) lines.add(s.substring(start));
150  
        break;
151  
      }
152  
153  
      lines.add(s.substring(start, i));
154  
      if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n')
155  
        i += 2;
156  
      else
157  
        ++i;
158  
159  
      start = i;
160  
    }
161  
    return lines;
162  
  }
163  
164  
  private static int toLines_nextLineBreak(String s, int start) {
165  
    for (int i = start; i < s.length(); i++) {
166  
      char c = s.charAt(i);
167  
      if (c == '\r' || c == '\n')
168  
        return i;
169  
    }
170  
    return -1;
171  
  }
172  
173  
  public static String fromLines(List<String> lines) {
174  
    StringBuilder buf = new StringBuilder();
175  
    for (String line : lines) {
176  
      buf.append(line).append('\n');
177  
    }
178  
    return buf.toString();
179  
  }
180  
181  
  private static File applyTranslators(File x, List<String> translators, List<File> libraries_out) throws Exception {
182  
    for (String translator : translators)
183  
      x = applyTranslator(x, translator, libraries_out);
184  
    return x;
185  
  }
186  
187  
  // also takes a library
188  
  private static File applyTranslator(File x, String translator, List<File> libraries_out) throws Exception {
189  
    if (verbose)
190  
      System.out.println("Using translator " + translator + " on sources in " + x.getPath());
191  
192  
    File newDir = runTranslatorOnInput(translator, null, x, !verbose, libraries_out);
193  
194  
    if (!new File(newDir, "main.java").exists()) {
195  
      throw new Exception("Translator " + translator + " did not generate main.java");
196  
      // TODO: show translator output
197  
    }
198  
    if (verbose)
199  
      System.out.println("Translated with " + translator + " from " + x.getPath() + " to " + newDir.getPath());
200  
    x = newDir;
201  
    return x;
202  
  }
203  
204  
  private static File luaPrintToJavaPrint(File x) throws IOException {
205  
    File newDir = TempDirMaker_make();
206  
    String code = loadTextFile(new File(x, "main.java").getPath(), null);
207  
    code = luaPrintToJavaPrint(code);
208  
    if (verbose)
209  
      System.out.println(code);
210  
    saveTextFile(new File(newDir, "main.java").getPath(), code);
211  
    return newDir;
212  
  }
213  
214  
  public static String luaPrintToJavaPrint(String code) {
215  
    return ("\n" + code).replaceAll(
216  
      "(\n\\s*)print (\".*\")",
217  
      "$1System.out.println($2);").substring(1);
218  
  }
219  
220  
  public static File loadSnippetAsMainJava(String snippetID) throws IOException {
221  
    File srcDir = TempDirMaker_make();
222  
    saveTextFile(new File(srcDir, "main.java").getPath(), loadSnippet(snippetID));
223  
    return srcDir;
224  
  }
225  
226  
  public static File loadSnippetAsMainJavaVerified(String snippetID, String hash) throws IOException {
227  
    File srcDir = TempDirMaker_make();
228  
    saveTextFile(new File(srcDir, "main.java").getPath(), loadSnippetVerified(snippetID, hash));
229  
    return srcDir;
230  
  }
231  
232  
  /** returns output dir */
233  
  private static File runTranslatorOnInput(String snippetID, String hash, File input, boolean silent,
234  
                                           List<File> libraries_out) throws Exception {
235  
    File libraryFile = DiskSnippetCache_getLibrary(parseSnippetID(snippetID));
236  
    if (libraryFile != null) {
237  
      loadLibrary(snippetID, libraries_out, libraryFile);
238  
      return input;
239  
    }
240  
241  
    File srcDir = hash == null ? loadSnippetAsMainJava(snippetID)
242  
      : loadSnippetAsMainJavaVerified(snippetID, hash);
243  
244  
    long mainJavaSize = new File(srcDir, "main.java").length();
245  
246  
    if (mainJavaSize == 0) { // no text in snippet? assume it's a library
247  
      loadLibrary(snippetID, libraries_out, libraryFile);
248  
      return input;
249  
    }
250  
251  
    List<File> libraries = new ArrayList<File>();
252  
    srcDir = defaultTranslate(srcDir, libraries);
253  
    boolean runInProcess = false;
254  
    File ioBaseDir = TempDirMaker_make();
255  
256  
    if (virtualizeTranslators) {
257  
      if (verbose) System.out.println("Virtualizing translator");
258  
259  
      //srcDir = applyTranslator(srcDir, "#2000351"); // I/O-virtualize the translator
260  
      // that doesn't work because it recurses infinitely...
261  
262  
      // So we do it right here:
263  
      String s = loadTextFile(new File(srcDir, "main.java").getPath(), null);
264  
      s = s.replaceAll("new\\s+File\\(", "virtual.newFile(");
265  
      s = s.replaceAll("new\\s+FileInputStream\\(", "virtual.newFileInputStream(");
266  
      s = s.replaceAll("new\\s+FileOutputStream\\(", "virtual.newFileOutputStream(");
267  
      s += "\n\n" + loadSnippet("#2000355"); // load class virtual
268  
269  
      // change baseDir
270  
      s = s.replace("virtual_baseDir = \"\";",
271  
        "virtual_baseDir = " + javaQuote(ioBaseDir.getAbsolutePath()) + ";");
272  
273  
      // forward snippet cache
274  
      s = s.replace("static File DiskSnippetCache_dir;",
275  
        "static File DiskSnippetCache_dir = new File(" + javaQuote(DiskSnippetCache_dir.getAbsolutePath()) + ");");
276  
      s = s.replace("static boolean preferCached = false;", "static boolean preferCached = true;");
277  
278  
      if (verbose) {
279  
        System.out.println("==BEGIN VIRTUALIZED TRANSLATOR==");
280  
        System.out.println(s);
281  
        System.out.println("==END VIRTUALIZED TRANSLATOR==");
282  
      }
283  
      saveTextFile(new File(srcDir, "main.java").getPath(), s);
284  
285  
      // TODO: silence translator also
286  
      runInProcess = true;
287  
    }
288  
289  
    return runJavaX(ioBaseDir, srcDir, input, silent, runInProcess, libraries);
290  
  }
291  
292  
  private static void loadLibrary(String snippetID, List<File> libraries_out, File libraryFile) throws IOException {
293  
    if (verbose)
294  
      System.out.println("Assuming " + snippetID + " is a library.");
295  
296  
    if (libraryFile == null) {
297  
      byte[] data;
298  
      try {
299  
        URL url = new URL("http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + parseSnippetID(snippetID)
300  
          + "&contentType=application/binary");
301  
        System.err.println("Loading library: " + url);
302  
        data = loadBinaryPage(url.openConnection(), url);
303  
        if (verbose)
304  
          System.err.println("Bytes loaded: " + data.length);
305  
      } catch (FileNotFoundException e) {
306  
        throw new IOException("Binary snippet #" + snippetID + " not found or not public");
307  
      }
308  
      DiskSnippetCache_putLibrary(parseSnippetID(snippetID), data);
309  
      libraryFile = DiskSnippetCache_getLibrary(parseSnippetID(snippetID));
310  
    }
311  
312  
    if (!libraries_out.contains(libraryFile))
313  
      libraries_out.add(libraryFile);
314  
  }
315  
316  
  /** returns output dir */
317  
  private static File runJavaX(File ioBaseDir, File originalSrcDir, File originalInput,
318  
                               boolean silent, boolean runInProcess,
319  
                               List<File> libraries) throws Exception {
320  
    File srcDir = new File(ioBaseDir, "src");
321  
    File inputDir = new File(ioBaseDir, "input");
322  
    File outputDir = new File(ioBaseDir, "output");
323  
    copyInput(originalSrcDir, srcDir);
324  
    copyInput(originalInput, inputDir);
325  
    javax2(srcDir, ioBaseDir, silent, runInProcess, libraries);
326  
    return outputDir;
327  
  }
328  
329  
  private static void copyInput(File src, File dst) throws IOException {
330  
    copyDirectory(src, dst);
331  
  }
332  
333  
  public static boolean hasFile(File inputDir, String name) {
334  
    return new File(inputDir, name).exists();
335  
  }
336  
337  
  public static void copyDirectory(File src, File dst) throws IOException {
338  
    if (verbose) System.out.println("Copying " + src.getAbsolutePath() + " to " + dst.getAbsolutePath());
339  
    dst.mkdirs();
340  
    File[] files = src.listFiles();
341  
    if (files == null) return;
342  
    for (File file : files) {
343  
      File dst1 = new File(dst, file.getName());
344  
      if (file.isDirectory())
345  
        copyDirectory(file, dst1);
346  
      else {
347  
        if (verbose) System.out.println("Copying " + file.getAbsolutePath() + " to " + dst1.getAbsolutePath());
348  
        copy(file, dst1);
349  
      }
350  
    }
351  
  }
352  
353  
  /** Quickly copy a file without a progress bar or any other fancy GUI... :) */
354  
  public static void copy(File src, File dest) throws IOException {
355  
    FileInputStream inputStream = new FileInputStream(src);
356  
    FileOutputStream outputStream = new FileOutputStream(dest);
357  
    try {
358  
      copy(inputStream, outputStream);
359  
      inputStream.close();
360  
    } finally {
361  
      outputStream.close();
362  
    }
363  
  }
364  
365  
  public static void copy(InputStream in, OutputStream out) throws IOException {
366  
    byte[] buf = new byte[65536];
367  
    while (true) {
368  
      int n = in.read(buf);
369  
      if (n <= 0) return;
370  
      out.write(buf, 0, n);
371  
    }
372  
  }
373  
374  
  /** writes safely (to temp file, then rename) */
375  
  public static void saveTextFile(String fileName, String contents) throws IOException {
376  
    File file = new File(fileName);
377  
    File parentFile = file.getParentFile();
378  
    if (parentFile != null)
379  
      parentFile.mkdirs();
380  
    String tempFileName = fileName + "_temp";
381  
    FileOutputStream fileOutputStream = new FileOutputStream(tempFileName);
382  
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, charsetForTextFiles);
383  
    PrintWriter printWriter = new PrintWriter(outputStreamWriter);
384  
    printWriter.print(contents);
385  
    printWriter.close();
386  
    if (file.exists() && !file.delete())
387  
      throw new IOException("Can't delete " + fileName);
388  
389  
    if (!new File(tempFileName).renameTo(file))
390  
      throw new IOException("Can't rename " + tempFileName + " to " + fileName);
391  
  }
392  
393  
  /** writes safely (to temp file, then rename) */
394  
  public static void saveBinaryFile(String fileName, byte[] contents) throws IOException {
395  
    File file = new File(fileName);
396  
    File parentFile = file.getParentFile();
397  
    if (parentFile != null)
398  
      parentFile.mkdirs();
399  
    String tempFileName = fileName + "_temp";
400  
    FileOutputStream fileOutputStream = new FileOutputStream(tempFileName);
401  
    fileOutputStream.write(contents);
402  
    fileOutputStream.close();
403  
    if (file.exists() && !file.delete())
404  
      throw new IOException("Can't delete " + fileName);
405  
406  
    if (!new File(tempFileName).renameTo(file))
407  
      throw new IOException("Can't rename " + tempFileName + " to " + fileName);
408  
  }
409  
410  
  public static String loadTextFile(String fileName, String defaultContents) throws IOException {
411  
    if (!new File(fileName).exists())
412  
      return defaultContents;
413  
414  
    FileInputStream fileInputStream = new FileInputStream(fileName);
415  
    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charsetForTextFiles);
416  
    return loadTextFile(inputStreamReader);
417  
  }
418  
419  
  public static String loadTextFile(Reader reader) throws IOException {
420  
    StringBuilder builder = new StringBuilder();
421  
    try {
422  
      BufferedReader bufferedReader = new BufferedReader(reader);
423  
      String line;
424  
      while ((line = bufferedReader.readLine()) != null)
425  
        builder.append(line).append('\n');
426  
    } finally {
427  
      reader.close();
428  
    }
429  
    return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1);
430  
  }
431  
432  
  static File DiskSnippetCache_dir;
433  
434  
  public static void initDiskSnippetCache(File dir) {
435  
    DiskSnippetCache_dir = dir;
436  
    dir.mkdirs();
437  
  }
438  
439  
  // Data files are immutable, use centralized cache
440  
  public static synchronized File DiskSnippetCache_getLibrary(long snippetID) throws IOException {
441  
    File file = new File(getGlobalCache(), "data_" + snippetID + ".jar");
442  
    if (verbose)
443  
      System.out.println("Checking data cache: " + file.getPath());
444  
    return file.exists() ? file : null;
445  
  }
446  
447  
  public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException {
448  
    return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null);
449  
  }
450  
451  
  private static File DiskSnippetCache_getFile(long snippetID) {
452  
    return new File(DiskSnippetCache_dir, "" + snippetID);
453  
  }
454  
455  
  public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException {
456  
    saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet);
457  
  }
458  
459  
  public static synchronized void DiskSnippetCache_putLibrary(long snippetID, byte[] data) throws IOException {
460  
    saveBinaryFile(new File(getGlobalCache(), "data_" + snippetID).getPath() + ".jar", data);
461  
  }
462  
463  
  public static File DiskSnippetCache_getDir() {
464  
    return DiskSnippetCache_dir;
465  
  }
466  
467  
  public static void initSnippetCache() {
468  
    if (DiskSnippetCache_dir == null)
469  
      initDiskSnippetCache(getGlobalCache());
470  
  }
471  
472  
  private static File getGlobalCache() {
473  
    File file = new File(System.getProperty("user.home"), ".tinybrain/snippet-cache");
474  
    file.mkdirs();
475  
    return file;
476  
  }
477  
478  
  public static String loadSnippetVerified(String snippetID, String hash) throws IOException {
479  
    String text = loadSnippet(snippetID);
480  
    String realHash = getHash(text.getBytes("UTF-8"));
481  
    if (!realHash.equals(hash)) {
482  
      String msg;
483  
      if (hash.isEmpty())
484  
        msg = "Here's your hash for " + snippetID + ", please put in your program: " + realHash;
485  
      else
486  
        msg = "Hash mismatch for " + snippetID + ": " + realHash + " (new) vs " + hash + " - has tinybrain.de been hacked??";
487  
      throw new RuntimeException(msg);
488  
    }
489  
    return text;
490  
  }
491  
492  
  public static String getHash(byte[] data) {
493  
    return bytesToHex(getFullFingerprint(data));
494  
  }
495  
496  
  public static byte[] getFullFingerprint(byte[] data) {
497  
    try {
498  
      return MessageDigest.getInstance("MD5").digest(data);
499  
    } catch (NoSuchAlgorithmException e) {
500  
      throw new RuntimeException(e);
501  
    }
502  
  }
503  
504  
  public static String bytesToHex(byte[] bytes) {
505  
    return bytesToHex(bytes, 0, bytes.length);
506  
  }
507  
508  
  public static String bytesToHex(byte[] bytes, int ofs, int len) {
509  
    StringBuilder stringBuilder = new StringBuilder(len*2);
510  
    for (int i = 0; i < len; i++) {
511  
      String s = "0" + Integer.toHexString(bytes[ofs+i]);
512  
      stringBuilder.append(s.substring(s.length()-2, s.length()));
513  
    }
514  
    return stringBuilder.toString();
515  
  }
516  
517  
  public static String loadSnippet(String snippetID) throws IOException {
518  
    return loadSnippet(parseSnippetID(snippetID));
519  
  }
520  
521  
  public static long parseSnippetID(String snippetID) {
522  
    return Long.parseLong(shortenSnippetID(snippetID));
523  
  }
524  
525  
  private static String shortenSnippetID(String snippetID) {
526  
    if (snippetID.startsWith("#"))
527  
      snippetID = snippetID.substring(1);
528  
    String httpBlaBla = "http://tinybrain.de/";
529  
    if (snippetID.startsWith(httpBlaBla))
530  
      snippetID = snippetID.substring(httpBlaBla.length());
531  
    return snippetID;
532  
  }
533  
534  
  public static boolean isSnippetID(String snippetID) {
535  
    snippetID = shortenSnippetID(snippetID);
536  
    return isInteger(snippetID) && Long.parseLong(snippetID) != 0;
537  
  }
538  
539  
  public static boolean isInteger(String s) {
540  
    return Pattern.matches("\\-?\\d+", s);
541  
  }
542  
543  
  public static String loadSnippet(long snippetID) throws IOException {
544  
    String text = memSnippetCache.get(snippetID);
545  
    if (text != null)
546  
      return text;
547  
548  
    if (preferCached) {
549  
      initSnippetCache();
550  
      text = DiskSnippetCache_get(snippetID);
551  
      if (text != null)
552  
        return text;
553  
    }
554  
555  
    try {
556  
      URL url = new URL("http://tinybrain.de:8080/getraw.php?id=" + snippetID);
557  
      text = loadPage(url);
558  
    } catch (FileNotFoundException e) {
559  
      throw new IOException("Snippet #" + snippetID + " not found or not public");
560  
    }
561  
562  
    memSnippetCache.put(snippetID, text);
563  
564  
    try {
565  
      initSnippetCache();
566  
      DiskSnippetCache_put(snippetID, text);
567  
    } catch (IOException e) {
568  
      System.err.println("Minor warning: Couldn't save snippet to cache ("  + DiskSnippetCache_getDir() + ")");
569  
    }
570  
571  
    return text;
572  
  }
573  
574  
  private static String loadPage(URL url) throws IOException {
575  
    System.err.println("Loading: " + url.toExternalForm());
576  
    URLConnection con = url.openConnection();
577  
    return loadPage(con, url);
578  
  }
579  
580  
  public static String loadPage(URLConnection con, URL url) throws IOException {
581  
    String contentType = con.getContentType();
582  
    if (contentType == null)
583  
      throw new IOException("Page could not be read: " + url);
584  
    //Log.info("Content-Type: " + contentType);
585  
    String charset = guessCharset(contentType);
586  
    Reader r = new InputStreamReader(con.getInputStream(), charset);
587  
    StringBuilder buf = new StringBuilder();
588  
    while (true) {
589  
      int ch = r.read();
590  
      if (ch < 0)
591  
        break;
592  
      //Log.info("Chars read: " + buf.length());
593  
      buf.append((char) ch);
594  
    }
595  
    return buf.toString();
596  
  }
597  
598  
  public static byte[] loadBinaryPage(URLConnection con, URL url) throws IOException {
599  
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
600  
    InputStream inputStream = con.getInputStream();
601  
    while (true) {
602  
      int ch = inputStream.read();
603  
      if (ch < 0)
604  
        break;
605  
      buf.write(ch);
606  
    }
607  
    inputStream.close();
608  
    return buf.toByteArray();
609  
  }
610  
611  
  public static String guessCharset(String contentType) {
612  
    Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*");
613  
    Matcher m = p.matcher(contentType);
614  
    /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */
615  
    return m.matches() ? m.group(1) : "ISO-8859-1";
616  
  }
617  
618  
  /** runs a transpiled set of sources */
619  
  public static void javax2(File srcDir, File ioBaseDir, boolean silent, boolean runInProcess,
620  
                            List<File> libraries) throws Exception {
621  
    // collect sources
622  
623  
    List<File> sources = new ArrayList<File>();
624  
    if (verbose) System.out.println("Scanning for sources in " + srcDir.getPath());
625  
    scanForSources(srcDir, sources, true);
626  
    if (sources.isEmpty()) {
627  
      System.out.println("No sources found");
628  
      return;
629  
    }
630  
631  
    // compile
632  
633  
    File optionsFile = File.createTempFile("javax", "");
634  
    File classesDir = TempDirMaker_make();
635  
    if (verbose) System.out.println("Compiling " + sources.size() + " source(s) to " + classesDir.getPath());
636  
    String options = "-d " + bashQuote(classesDir.getPath());
637  
    writeOptions(sources, libraries, optionsFile, options);
638  
    classesDir.mkdirs();
639  
    String javacOutput = invokeJavac(optionsFile);
640  
641  
    // run
642  
643  
    if (verbose) System.out.println("Running program (" + srcDir.getAbsolutePath()
644  
      + ") on io dir " + ioBaseDir.getAbsolutePath() + (runInProcess ? "[in-process]" : "") + "\n");
645  
    runProgram(javacOutput, classesDir, ioBaseDir, silent, runInProcess, libraries);
646  
  }
647  
648  
  private static void runProgram(String javacOutput, File classesDir, File ioBaseDir,
649  
                                 boolean silent, boolean runInProcess,
650  
                                 List<File> libraries) throws Exception {
651  
    // print javac output if compile failed and it hasn't been printed yet
652  
    boolean didNotCompile = !hasFile(classesDir, "main.class");
653  
    if (verbose || didNotCompile)
654  
      System.out.println(javacOutput);
655  
    if (didNotCompile)
656  
      return;
657  
658  
    if (runInProcess
659  
      || (ioBaseDir.getAbsolutePath().equals(new File(".").getAbsolutePath()) && !silent)) {
660  
      runProgramQuick(classesDir, libraries);
661  
      return;
662  
    }
663  
664  
    boolean echoOK = false;
665  
    // TODO: add libraries to class path
666  
    String bashCmd = "(cd " + bashQuote(ioBaseDir.getAbsolutePath()) + " && (java -cp "
667  
      + bashQuote(classesDir.getAbsolutePath()) + " main" + (echoOK ? "; echo ok" : "") + "))";
668  
    if (verbose) System.out.println(bashCmd);
669  
    String output = backtick(bashCmd);
670  
    if (verbose || !silent)
671  
      System.out.println(output);
672  
  }
673  
674  
  private static void runProgramQuick(File classesDir, List<File> libraries) throws Exception {
675  
    // collect urls
676  
    URL[] urls = new URL[libraries.size()+1];
677  
    urls[0] = classesDir.toURI().toURL();
678  
    for (int i = 0; i < libraries.size(); i++)
679  
      urls[i+1] = libraries.get(i).toURI().toURL();
680  
681  
    // make class loader
682  
    URLClassLoader classLoader = new URLClassLoader(urls);
683  
684  
    // load JavaX main class
685  
    Class<?> mainClass = classLoader.loadClass("main");
686  
687  
    // run main method
688  
    Method main = mainClass.getMethod("main", String[].class);
689  
    main.invoke(null, (Object) new String[0]);
690  
  }
691  
692  
  private static String invokeJavac(File optionsFile) throws IOException {
693  
    String output;
694  
    try {
695  
      output = invokeEcj(optionsFile);
696  
    } catch (NoClassDefFoundError e) {
697  
      if (verbose) {
698  
        System.err.println("ecj not found - using javac");
699  
        e.printStackTrace();
700  
      }
701  
      output = backtick("javac " + bashQuote("@" + optionsFile.getPath()));
702  
    }
703  
    if (verbose) System.out.println(output);
704  
    return output;
705  
  }
706  
707  
  // throws ClassNotFoundError if ecj is not in classpath
708  
  static String invokeEcj(File optionsFile) {
709  
    StringWriter writer = new StringWriter();
710  
    PrintWriter printWriter = new PrintWriter(writer);
711  
    org.eclipse.jdt.core.compiler.CompilationProgress progress = null;
712  
    org.eclipse.jdt.core.compiler.batch.BatchCompiler.compile(
713  
      new String[] { "@" + optionsFile.getPath(), "-source", "1.7" },
714  
      printWriter,
715  
      printWriter,
716  
      progress);
717  
    return writer.toString();
718  
  }
719  
720  
  private static void writeOptions(List<File> sources, List<File> libraries,
721  
                                   File optionsFile, String moreOptions) throws IOException {
722  
    FileWriter writer = new FileWriter(optionsFile);
723  
    for (File source : sources)
724  
      writer.write(bashQuote(source.getPath()) + " ");
725  
    if (!libraries.isEmpty()) {
726  
      List<String> cp = new ArrayList<String>();
727  
      for (File lib : libraries)
728  
        cp.add(lib.getAbsolutePath());
729  
      writer.write("-cp " + bashQuote(join(File.pathSeparator, cp)) + " ");
730  
    }
731  
    writer.write(moreOptions);
732  
    writer.close();
733  
  }
734  
735  
  private static void scanForSources(File source, List<File> sources, boolean topLevel) {
736  
    if (source.isFile() && source.getName().endsWith(".java"))
737  
      sources.add(source);
738  
    else if (source.isDirectory() && !isSkippedDirectoryName(source.getName(), topLevel)) {
739  
      File[] files = source.listFiles();
740  
      for (File file : files)
741  
        scanForSources(file, sources, false);
742  
    }
743  
  }
744  
745  
  private static boolean isSkippedDirectoryName(String name, boolean topLevel) {
746  
    if (topLevel) return false; // input or output ok as highest directory (intentionally specified by user, not just found by a directory scan in which case we probably don't want it. it's more like heuristics actually.)
747  
    return name.equalsIgnoreCase("input") || name.equalsIgnoreCase("output");
748  
  }
749  
750  
  public static String backtick(String cmd) throws IOException {
751  
    ++processesStarted;
752  
    File outFile = File.createTempFile("_backtick", "");
753  
    File scriptFile = File.createTempFile("_backtick", isWindows() ? ".bat" : "");
754  
755  
    String command = cmd + ">" + bashQuote(outFile.getPath()) + " 2>&1";
756  
    //Log.info("[Backtick] " + command);
757  
    try {
758  
      saveTextFile(scriptFile.getPath(), command);
759  
      String[] command2;
760  
      if (isWindows())
761  
        command2 = new String[] { scriptFile.getPath() };
762  
      else
763  
        command2 = new String[] { "/bin/bash", scriptFile.getPath() };
764  
      Process process = Runtime.getRuntime().exec(command2);
765  
      try {
766  
        process.waitFor();
767  
      } catch (InterruptedException e) {
768  
        throw new RuntimeException(e);
769  
      }
770  
      process.exitValue();
771  
      return loadTextFile(outFile.getPath(), "");
772  
    } finally {
773  
      scriptFile.delete();
774  
    }
775  
  }
776  
777  
  /** possibly improvable */
778  
  public static String javaQuote(String text) {
779  
    return bashQuote(text);
780  
  }
781  
782  
  /** possibly improvable */
783  
  public static String bashQuote(String text) {
784  
    if (text == null) return null;
785  
    return "\"" + text
786  
      .replace("\\", "\\\\")
787  
      .replace("\"", "\\\"")
788  
      .replace("\n", "\\n")
789  
      .replace("\r", "\\r") + "\"";
790  
  }
791  
792  
  public final static String charsetForTextFiles = "UTF8";
793  
794  
  static long TempDirMaker_lastValue;
795  
796  
  public static File TempDirMaker_make() {
797  
    File dir = new File(System.getProperty("user.home"), ".javax/" + TempDirMaker_newValue());
798  
    dir.mkdirs();
799  
    return dir;
800  
  }
801  
802  
  private static long TempDirMaker_newValue() {
803  
    long value;
804  
    do
805  
      value = System.currentTimeMillis();
806  
    while (value == TempDirMaker_lastValue);
807  
    TempDirMaker_lastValue = value;
808  
    return value;
809  
  }
810  
811  
  public static String join(String glue, Iterable<String> strings) {
812  
    StringBuilder buf = new StringBuilder();
813  
    Iterator<String> i = strings.iterator();
814  
    if (i.hasNext()) {
815  
      buf.append(i.next());
816  
      while (i.hasNext())
817  
        buf.append(glue).append(i.next());
818  
    }
819  
    return buf.toString();
820  
  }
821  
822  
  public static boolean isWindows() {
823  
    return System.getProperty("os.name").contains("Windows");
824  
  }
825  
}

download  show line numbers   

Snippet is not live.

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #2000358
Snippet name: x12.java
Eternal ID of this version: #2000358/1
Text MD5: 71dc4dd0495a3ea33582102c5661264a
Author: stefan
Category: javax
Type: New Tinybrain snippet
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-05-21 18:10:30
Source code size: 29817 bytes / 825 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 656 / 115
Referenced in: [show references]