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

814
LINES

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

New Tinybrain snippet

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

Author comment

Began life as a copy of #2000358

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: #2000359
Snippet name: x12.java
Eternal ID of this version: #2000359/1
Text MD5: 0e625db74d7a418dbda6e7baf9954ae7
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:13:25
Source code size: 29521 bytes / 814 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 650 / 133
Referenced in: [show references]