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

851
LINES

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

New Tinybrain snippet

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

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: #2000388
Snippet name: class x14 (embeddable)
Eternal ID of this version: #2000388/1
Text MD5: 467534297e254bb7ac835f05b26f76cb
Author: stefan
Category:
Type: New Tinybrain snippet
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-06-25 19:53:55
Source code size: 31007 bytes / 851 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 515 / 128
Referenced in: [show references]