rt/vm/src/main/java/org/apidesign/vm4brwsr/Main.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 27 May 2014 12:25:41 +0200
branchclosure
changeset 1604 7665471a56c1
parent 1513 ba912ef24b27
child 1762 293838e72201
permissions -rw-r--r--
The static calculator demo needs to reference just a single application .js file from its main HTML page. The rest is loaded based on classpath attribute.
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import java.io.BufferedWriter;
    21 import java.io.File;
    22 import java.io.FileWriter;
    23 import java.io.IOException;
    24 import java.io.Writer;
    25 import java.net.URI;
    26 import java.net.URISyntaxException;
    27 import java.net.URL;
    28 import java.util.Enumeration;
    29 import java.util.jar.JarEntry;
    30 import java.util.jar.JarFile;
    31 
    32 /** Generator of JavaScript from bytecode of classes on classpath of the VM
    33  * with a Main method.
    34  *
    35  * @author Jaroslav Tulach <jtulach@netbeans.org>
    36  */
    37 final class Main {
    38     private Main() {}
    39     
    40     public static void main(String... args) throws IOException, URISyntaxException {
    41         final String obfuscate = "--obfuscatelevel";
    42         final String extension = "--createextension";
    43 
    44         if (args.length < 2) {
    45             System.err.println("Bck2Brwsr Translator from Java(tm) to JavaScript, (c) Jaroslav Tulach 2012");
    46             System.err.print("Usage: java -cp ... -jar ... [");
    47             System.err.print(obfuscate);
    48             System.err.print(" [");
    49             boolean first = true;
    50             for (ObfuscationLevel l : ObfuscationLevel.values()) {
    51                 if (!first) {
    52                     System.err.print('|');
    53                 }
    54                 System.err.print(l.name());
    55                 first = false;
    56             }
    57             System.err.print("]] [");
    58             System.err.print(extension);
    59             System.err.println("] <file_to_generate_js_code_to> java/lang/Class org/your/App ...");
    60             System.exit(9);
    61         }
    62 
    63         final ClassLoader mainClassLoader = Main.class.getClassLoader();
    64 
    65         ObfuscationLevel obfLevel = ObfuscationLevel.NONE;
    66         boolean createExtension = false;
    67         StringArray classes = new StringArray();
    68         String generateTo = null;
    69         for (int i = 0; i < args.length; i++) {
    70             if (obfuscate.equals(args[i])) { // NOI18N
    71                 i++;
    72                 try {
    73                     obfLevel = ObfuscationLevel.valueOf(args[i]);
    74                 } catch (Exception e) {
    75                     System.err.print(obfuscate);
    76                     System.err.print(" parameter needs to be followed by one of ");
    77                     boolean first = true;
    78                     for (ObfuscationLevel l : ObfuscationLevel.values()) {
    79                         if (!first) {
    80                             System.err.print(", ");
    81                         }
    82                         System.err.print(l.name());
    83                         first = false;
    84                     }
    85                     System.err.println();
    86                     System.exit(1);
    87                 }
    88                 continue;
    89             }
    90             if (extension.equals(args[i])) { // NOI18N
    91                 createExtension = true;
    92                 continue;
    93             }
    94             if (generateTo == null) {
    95                 generateTo = args[i];
    96             } else {
    97                 collectClasses(classes, mainClassLoader, args[i]);
    98             }
    99         }
   100         
   101         File gt = new File(generateTo);
   102         if (Boolean.getBoolean("skip.if.exists") && gt.isFile()) {
   103             System.err.println("Skipping as " + gt + " exists.");
   104             System.exit(0);
   105         }
   106         
   107         try (Writer w = new BufferedWriter(new FileWriter(gt))) {
   108             Bck2Brwsr c = Bck2Brwsr.newCompiler().
   109                 obfuscation(obfLevel).
   110                 addRootClasses(classes.toArray()).
   111                 resources(new LdrRsrcs(Main.class.getClassLoader(), true));
   112             
   113             if (createExtension) {
   114                 c = c.library();
   115             }
   116             
   117             c.generate(w);
   118         }
   119     }
   120 
   121     private static void collectClasses(
   122             final StringArray dest,
   123             final ClassLoader cl, final String relativePath)
   124                 throws IOException, URISyntaxException {
   125         final Enumeration<URL> urls = cl.getResources(relativePath);
   126         if (!urls.hasMoreElements()) {
   127             dest.add(relativePath);
   128             return;
   129         }
   130         do {
   131             final URL url = urls.nextElement();
   132             switch (url.getProtocol()) {
   133                 case "file":
   134                     collectClasses(dest, relativePath,
   135                                    new File(new URI(url.toString())));
   136                     continue;
   137                 case "jar":
   138                     final String fullPath = url.getPath();
   139                     final int sepIndex = fullPath.indexOf('!');
   140                     final String jarFilePath =
   141                             (sepIndex != -1) ? fullPath.substring(0, sepIndex)
   142                                              : fullPath;
   143 
   144                     final URI jarUri = new URI(jarFilePath);
   145                     if (jarUri.getScheme().equals("file")) {
   146                         try (JarFile jarFile = new JarFile(new File(jarUri))) {
   147                             collectClasses(dest, relativePath, jarFile);
   148                             continue;
   149                         }
   150                     }
   151                     break;
   152             }
   153 
   154             dest.add(relativePath);
   155         } while (urls.hasMoreElements());
   156     }
   157 
   158     private static void collectClasses(final StringArray dest,
   159                                        final String relativePath,
   160                                        final File file) {
   161         if (file.isDirectory()) {
   162             final File[] subFiles = file.listFiles();
   163             for (final File subFile: subFiles) {
   164                 collectClasses(dest,
   165                                extendPath(relativePath, subFile.getName()),
   166                                subFile);
   167             }
   168 
   169             return;
   170         }
   171 
   172         final String filePath = file.getPath();
   173         if (filePath.endsWith(".class")) {
   174             validateAndAddClass(dest, relativePath);
   175         }
   176     }
   177 
   178     private static void collectClasses(final StringArray dest,
   179                                        final String relativePath,
   180                                        final JarFile jarFile) {
   181         if (relativePath.endsWith(".class")) {
   182             if (jarFile.getJarEntry(relativePath) != null) {
   183                 validateAndAddClass(dest, relativePath);
   184             }
   185 
   186             return;
   187         }
   188 
   189         final String expectedPrefix =
   190                 relativePath.endsWith("/") ? relativePath
   191                                            : relativePath + '/';
   192         final Enumeration<JarEntry> entries = jarFile.entries();
   193         while (entries.hasMoreElements()) {
   194             final JarEntry entry = entries.nextElement();
   195             if (!entry.isDirectory()) {
   196                 final String entryName = entry.getName();
   197                 if (entryName.startsWith(expectedPrefix)
   198                         && entryName.endsWith(".class")) {
   199                     validateAndAddClass(dest, entryName);
   200                 }
   201             }
   202         }
   203     }
   204 
   205     private static String extendPath(final String relativePath,
   206                                      final String fileName) {
   207         return relativePath.endsWith("/") ? relativePath + fileName
   208                                           : relativePath + '/' + fileName;
   209     }
   210 
   211     private static void validateAndAddClass(final StringArray dest,
   212                                             final String relativePath) {
   213         final String className =
   214                 relativePath.substring(0, relativePath.length() - 6);
   215         if (!className.endsWith("package-info")) {
   216             dest.add(className);
   217         }
   218     }
   219 }