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

864
LINES

< > BotCompany Repo | #2000390 // class x14 (embeddable)

New Tinybrain snippet

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

Author comment

Began life as a copy of #2000389

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: #2000390
Snippet name: class x14 (embeddable)
Eternal ID of this version: #2000390/1
Text MD5: 80b9112dbee4b3e7d17e787ac1041ab5
Author: stefan
Category:
Type: New Tinybrain snippet
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-06-25 20:48:03
Source code size: 31462 bytes / 864 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 558 / 386
Referenced in: [show references]