2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 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.FileWriter;
23 import java.io.IOException;
24 import java.io.Writer;
26 import java.net.URISyntaxException;
28 import java.util.Enumeration;
29 import java.util.jar.JarEntry;
30 import java.util.jar.JarFile;
32 /** Generator of JavaScript from bytecode of classes on classpath of the VM
35 * @author Jaroslav Tulach <jtulach@netbeans.org>
40 public static void main(String... args) throws IOException, URISyntaxException {
41 final String obfuscate = "--obfuscatelevel";
43 if (args.length < 2) {
44 System.err.println("Bck2Brwsr Translator from Java(tm) to JavaScript, (c) Jaroslav Tulach 2012");
45 System.err.println("Usage: java -cp ... -jar ... [");
46 System.err.print(obfuscate);
47 System.err.print(" [");
49 for (ObfuscationLevel l : ObfuscationLevel.values()) {
51 System.err.print('|');
53 System.err.print(l.name());
57 System.err.println("] <file_to_generate_js_code_to> java/lang/Class org/your/App ...");
61 final ClassLoader mainClassLoader = Main.class.getClassLoader();
63 ObfuscationLevel obfLevel = ObfuscationLevel.NONE;
64 StringArray classes = new StringArray();
65 String generateTo = null;
66 for (int i = 0; i < args.length; i++) {
67 if (obfuscate.equals(args[i])) { // NOI18N
70 obfLevel = ObfuscationLevel.valueOf(args[i]);
71 } catch (Exception e) {
72 System.err.print(obfuscate);
73 System.err.print(" parameter needs to be followed by one of ");
75 for (ObfuscationLevel l : ObfuscationLevel.values()) {
77 System.err.print(", ");
79 System.err.print(l.name());
87 if (generateTo == null) {
90 collectClasses(classes, mainClassLoader, args[i]);
93 try (Writer w = new BufferedWriter(new FileWriter(generateTo))) {
94 Bck2Brwsr.newCompiler().
95 obfuscation(obfLevel).
96 addRootClasses(classes.toArray()).
97 resources(mainClassLoader).
102 private static void collectClasses(
103 final StringArray dest,
104 final ClassLoader cl, final String relativePath)
105 throws IOException, URISyntaxException {
106 final Enumeration<URL> urls = cl.getResources(relativePath);
107 if (!urls.hasMoreElements()) {
108 dest.add(relativePath);
112 final URL url = urls.nextElement();
113 switch (url.getProtocol()) {
115 collectClasses(dest, relativePath,
116 new File(new URI(url.toString())));
119 final String fullPath = url.getPath();
120 final int sepIndex = fullPath.indexOf('!');
121 final String jarFilePath =
122 (sepIndex != -1) ? fullPath.substring(0, sepIndex)
125 final URI jarUri = new URI(jarFilePath);
126 if (jarUri.getScheme().equals("file")) {
127 try (JarFile jarFile = new JarFile(new File(jarUri))) {
128 collectClasses(dest, relativePath, jarFile);
135 dest.add(relativePath);
136 } while (urls.hasMoreElements());
139 private static void collectClasses(final StringArray dest,
140 final String relativePath,
142 if (file.isDirectory()) {
143 final File[] subFiles = file.listFiles();
144 for (final File subFile: subFiles) {
146 extendPath(relativePath, subFile.getName()),
153 final String filePath = file.getPath();
154 if (filePath.endsWith(".class")) {
155 validateAndAddClass(dest, relativePath);
159 private static void collectClasses(final StringArray dest,
160 final String relativePath,
161 final JarFile jarFile) {
162 if (relativePath.endsWith(".class")) {
163 if (jarFile.getJarEntry(relativePath) != null) {
164 validateAndAddClass(dest, relativePath);
170 final String expectedPrefix =
171 relativePath.endsWith("/") ? relativePath
172 : relativePath + '/';
173 final Enumeration<JarEntry> entries = jarFile.entries();
174 while (entries.hasMoreElements()) {
175 final JarEntry entry = entries.nextElement();
176 if (!entry.isDirectory()) {
177 final String entryName = entry.getName();
178 if (entryName.startsWith(expectedPrefix)
179 && entryName.endsWith(".class")) {
180 validateAndAddClass(dest, entryName);
186 private static String extendPath(final String relativePath,
187 final String fileName) {
188 return relativePath.endsWith("/") ? relativePath + fileName
189 : relativePath + '/' + fileName;
192 private static void validateAndAddClass(final StringArray dest,
193 final String relativePath) {
194 final String className =
195 relativePath.substring(0, relativePath.length() - 6);
196 if (!className.endsWith("package-info")) {