2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.vm4brwsr;
20 import java.io.BufferedWriter;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStreamWriter;
25 import java.io.Writer;
27 import java.net.URISyntaxException;
29 import java.util.Enumeration;
30 import java.util.jar.JarEntry;
31 import java.util.jar.JarFile;
32 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
34 /** Generator of JavaScript from bytecode of classes on classpath of the VM
37 * @author Jaroslav Tulach <jtulach@netbeans.org>
39 @ExtraJavaScript(processByteCode = false, resource="")
43 public static void main(String... args) throws IOException, URISyntaxException {
44 final String obfuscate = "--obfuscatelevel";
45 final String extension = "--createextension";
47 if (args.length < 2) {
48 System.err.println("Bck2Brwsr Translator from Java(tm) to JavaScript, (c) Jaroslav Tulach 2012");
49 System.err.print("Usage: java -cp ... -jar ... [");
50 System.err.print(obfuscate);
51 System.err.print(" [");
53 for (ObfuscationLevel l : ObfuscationLevel.values()) {
55 System.err.print('|');
57 System.err.print(l.name());
60 System.err.print("]] [");
61 System.err.print(extension);
62 System.err.println("] <file_to_generate_js_code_to> java/lang/Class org/your/App ...");
66 final ClassLoader mainClassLoader = Main.class.getClassLoader();
68 ObfuscationLevel obfLevel = ObfuscationLevel.NONE;
69 boolean createExtension = false;
70 StringArray classes = new StringArray();
71 String generateTo = null;
72 for (int i = 0; i < args.length; i++) {
73 if (obfuscate.equals(args[i])) { // NOI18N
76 obfLevel = ObfuscationLevel.valueOf(args[i]);
77 } catch (Exception e) {
78 System.err.print(obfuscate);
79 System.err.print(" parameter needs to be followed by one of ");
81 for (ObfuscationLevel l : ObfuscationLevel.values()) {
83 System.err.print(", ");
85 System.err.print(l.name());
93 if (extension.equals(args[i])) { // NOI18N
94 createExtension = true;
97 if (generateTo == null) {
100 collectClasses(classes, mainClassLoader, args[i]);
104 File gt = new File(generateTo);
105 if (Boolean.getBoolean("skip.if.exists") && gt.isFile()) {
106 System.err.println("Skipping as " + gt + " exists.");
110 try (Writer w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(gt), "UTF-8"))) {
111 Bck2Brwsr c = Bck2Brwsr.newCompiler().
112 obfuscation(obfLevel).
113 addRootClasses(classes.toArray()).
114 resources(new LdrRsrcs(Main.class.getClassLoader(), true));
116 if (createExtension) {
124 private static void collectClasses(
125 final StringArray dest,
126 final ClassLoader cl, final String relativePath)
127 throws IOException, URISyntaxException {
128 final Enumeration<URL> urls = cl.getResources(relativePath);
129 if (!urls.hasMoreElements()) {
130 dest.add(relativePath);
134 final URL url = urls.nextElement();
135 switch (url.getProtocol()) {
137 collectClasses(dest, relativePath,
138 new File(new URI(url.toString())));
141 final String fullPath = url.getPath();
142 final int sepIndex = fullPath.indexOf('!');
143 final String jarFilePath =
144 (sepIndex != -1) ? fullPath.substring(0, sepIndex)
147 final URI jarUri = new URI(jarFilePath);
148 if (jarUri.getScheme().equals("file")) {
149 try (JarFile jarFile = new JarFile(new File(jarUri))) {
150 collectClasses(dest, relativePath, jarFile);
157 dest.add(relativePath);
158 } while (urls.hasMoreElements());
161 private static void collectClasses(final StringArray dest,
162 final String relativePath,
164 if (file.isDirectory()) {
165 final File[] subFiles = file.listFiles();
166 for (final File subFile: subFiles) {
168 extendPath(relativePath, subFile.getName()),
175 final String filePath = file.getPath();
176 if (filePath.endsWith(".class")) {
177 validateAndAddClass(dest, relativePath);
181 private static void collectClasses(final StringArray dest,
182 final String relativePath,
183 final JarFile jarFile) {
184 if (relativePath.endsWith(".class")) {
185 if (jarFile.getJarEntry(relativePath) != null) {
186 validateAndAddClass(dest, relativePath);
192 final String expectedPrefix =
193 relativePath.endsWith("/") ? relativePath
194 : relativePath + '/';
195 final Enumeration<JarEntry> entries = jarFile.entries();
196 while (entries.hasMoreElements()) {
197 final JarEntry entry = entries.nextElement();
198 if (!entry.isDirectory()) {
199 final String entryName = entry.getName();
200 if (entryName.startsWith(expectedPrefix)
201 && entryName.endsWith(".class")) {
202 validateAndAddClass(dest, entryName);
208 private static String extendPath(final String relativePath,
209 final String fileName) {
210 return relativePath.endsWith("/") ? relativePath + fileName
211 : relativePath + '/' + fileName;
214 private static void validateAndAddClass(final StringArray dest,
215 final String relativePath) {
216 final String className =
217 relativePath.substring(0, relativePath.length() - 6);