jaroslav@106: /** jaroslav@106: * Back 2 Browser Bytecode Translator jaroslav@106: * Copyright (C) 2012 Jaroslav Tulach jaroslav@106: * jaroslav@106: * This program is free software: you can redistribute it and/or modify jaroslav@106: * it under the terms of the GNU General Public License as published by jaroslav@106: * the Free Software Foundation, version 2 of the License. jaroslav@106: * jaroslav@106: * This program is distributed in the hope that it will be useful, jaroslav@106: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@106: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@106: * GNU General Public License for more details. jaroslav@106: * jaroslav@106: * You should have received a copy of the GNU General Public License jaroslav@106: * along with this program. Look for COPYING file in the top folder. jaroslav@106: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@106: */ jaroslav@29: package org.apidesign.vm4brwsr; jaroslav@29: jaroslav@29: import java.io.BufferedWriter; jaroslav@1058: import java.io.File; jaroslav@29: import java.io.FileWriter; jaroslav@29: import java.io.IOException; jaroslav@29: import java.io.Writer; jaroslav@29: jaroslav@136: /** Generator of JavaScript from bytecode of classes on classpath of the VM jaroslav@136: * with a Main method. jaroslav@29: * jaroslav@29: * @author Jaroslav Tulach jaroslav@29: */ jaroslav@136: final class Main { jaroslav@136: private Main() {} jaroslav@29: jaroslav@29: public static void main(String... args) throws IOException { jaroslav@881: final String obfuscate = "--obfuscatelevel"; jaroslav@881: jaroslav@29: if (args.length < 2) { jaroslav@298: System.err.println("Bck2Brwsr Translator from Java(tm) to JavaScript, (c) Jaroslav Tulach 2012"); jaroslav@881: System.err.println("Usage: java -cp ... -jar ... ["); jaroslav@881: System.err.print(obfuscate); jaroslav@881: System.err.print(" ["); jaroslav@881: boolean first = true; jaroslav@881: for (ObfuscationLevel l : ObfuscationLevel.values()) { jaroslav@881: if (!first) { jaroslav@881: System.err.print('|'); jaroslav@881: } jaroslav@881: System.err.print(l.name()); jaroslav@881: first = false; jaroslav@881: } jaroslav@881: jaroslav@881: System.err.println("] java/lang/Class org/your/App ..."); jaroslav@881: System.exit(9); jaroslav@29: } jaroslav@29: jaroslav@881: ObfuscationLevel obfLevel = ObfuscationLevel.NONE; jaroslav@881: StringArray classes = new StringArray(); jaroslav@881: String generateTo = null; jaroslav@881: for (int i = 0; i < args.length; i++) { jaroslav@881: if (obfuscate.equals(args[i])) { // NOI18N jaroslav@881: i++; jaroslav@881: try { jaroslav@881: obfLevel = ObfuscationLevel.valueOf(args[i]); jaroslav@881: } catch (Exception e) { jaroslav@881: System.err.print(obfuscate); jaroslav@881: System.err.print(" parameter needs to be followed by one of "); jaroslav@881: boolean first = true; jaroslav@881: for (ObfuscationLevel l : ObfuscationLevel.values()) { jaroslav@881: if (!first) { jaroslav@881: System.err.print(", "); jaroslav@881: } jaroslav@881: System.err.print(l.name()); jaroslav@881: first = false; jaroslav@881: } jaroslav@881: System.err.println(); jaroslav@881: System.exit(1); jaroslav@881: } jaroslav@881: continue; jaroslav@881: } jaroslav@881: if (generateTo == null) { jaroslav@881: generateTo = args[i]; jaroslav@881: } else { jaroslav@881: classes = classes.addAndNew(args[i]); jaroslav@881: } jaroslav@881: } jaroslav@1058: jaroslav@1058: File gt = new File(generateTo); jaroslav@1058: if (Boolean.getBoolean("skip.if.exists") && gt.isFile()) { jaroslav@1058: System.err.println("Skipping as " + gt + " exists."); jaroslav@1058: System.exit(0); jaroslav@1058: } jaroslav@1058: jaroslav@1058: try (Writer w = new BufferedWriter(new FileWriter(gt))) { jaroslav@881: Bck2Brwsr.newCompiler(). jaroslav@881: obfuscation(obfLevel). jaroslav@881: addRootClasses(classes.toArray()). jaroslav@881: resources(Main.class.getClassLoader()). jaroslav@881: generate(w); lubomir@281: } jaroslav@29: } jaroslav@29: }