1 20 21 package com.tonicsystems.jarjar; 22 23 import com.tonicsystems.jarjar.util.*; 24 import gnu.getopt.Getopt; 25 import gnu.getopt.LongOpt; 26 import java.io.*; 27 import java.util.*; 28 29 public class Main 30 { 31 public static final int STYLE_SIMPLE = 0; 32 private static final String HELP; 33 34 static { 35 try { 36 HELP = IoUtils.readIntoString(Main.class.getResourceAsStream("help.txt")); 37 } catch (IOException e) { 38 throw new RuntimeIOException(e); 39 } 40 } 41 42 private boolean verbose; 43 private List patterns; 44 private int level = DepHandler.LEVEL_CLASS; 45 private int style = STYLE_SIMPLE; 46 47 public static void main(String [] args) throws IOException { 49 if (args.length == 0) { 50 help(); 51 return; 52 } 53 54 Getopt g = new Getopt("jarjar", args, ":fhvs", new LongOpt[]{ 55 new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'), 56 new LongOpt("verbose", LongOpt.NO_ARGUMENT, null, 'v'), 57 new LongOpt("rules", LongOpt.REQUIRED_ARGUMENT, null, 2), 58 new LongOpt("find", LongOpt.NO_ARGUMENT, null, 'f'), 59 new LongOpt("strings", LongOpt.NO_ARGUMENT, null, 's'), 60 new LongOpt("level", LongOpt.REQUIRED_ARGUMENT, null, 3), 61 new LongOpt("style", LongOpt.REQUIRED_ARGUMENT, null, 4), 62 }); 63 64 boolean find = false; 65 boolean strings = false; 66 try { 67 Main main = new Main(); 68 int c; 69 while ((c = g.getopt()) != -1) { 70 switch (c) { 71 case 2: 72 main.setRules(new File(g.getOptarg())); 73 break; 74 case 3: 75 String level = g.getOptarg(); 76 if ("jar".equals(level)) { 77 main.setLevel(DepHandler.LEVEL_JAR); 78 } else if ("class".equals(level)) { 79 main.setLevel(DepHandler.LEVEL_CLASS); 80 } else { 81 throw new IllegalArgumentException ("unknown level " + level); 82 } 83 break; 84 case 4: 85 String style = g.getOptarg(); 86 if ("simple".equals(style)) { 87 main.setStyle(STYLE_SIMPLE); 88 } else { 89 throw new IllegalArgumentException ("unknown style " + style); 90 } 91 break; 92 case 's': 93 strings = true; 94 break; 95 case 'f': 96 find = true; 97 break; 98 case 'h': 99 help(); 100 return; 101 case 'v': 102 main.setVerbose(true); 103 break; 104 } 105 } 106 if (find && strings) 107 throw new IllegalArgumentException ("find and strings cannot be used together"); 108 int index = g.getOptind(); 109 int argCount = args.length - index; 110 if (argCount == 2) { 111 if (find) { 112 main.find(args[index], args[index + 1]); 113 } else { 114 main.run(args[index], args[index + 1]); 115 } 116 } else if (find) { 117 main.find(args[index]); 118 } else if (strings) { 119 main.strings(args[index]); 120 } else { 121 System.err.println("jarjar: expected two arguments"); 122 } 123 } catch (IllegalArgumentException e) { 124 System.err.println("jarjar: " + e.getMessage()); 125 return; 126 } 127 } 128 129 private static void help() { 130 System.err.print(HELP); 131 } 132 133 public void setLevel(int level) { 134 this.level = level; 135 } 136 137 public void setStyle(int style) { 138 this.style = style; 139 } 140 141 public void setRules(File file) throws IOException { 142 if (file == null) 143 throw new IllegalArgumentException ("rules cannot be null"); 144 patterns = RulesFileParser.parse(file); 145 } 146 147 public void setRules(List rules) { 148 patterns = new ArrayList(rules); 149 } 150 151 public void setVerbose(boolean verbose) { 152 this.verbose = verbose; 153 } 154 155 public void find(String arg) throws IOException { 156 find(arg, arg); 157 } 158 159 public void find(String from, String to) throws IOException { 160 if (from == null || to == null) 161 throw new IllegalArgumentException ("arguments cannot be null"); 162 if (patterns != null) 163 throw new IllegalArgumentException ("rules cannot be used with find"); 164 PrintWriter w = new PrintWriter(System.out); 165 DepHandler handler = new TextDepHandler(w, level); 166 new DepFind().run(from, to, handler); 167 w.flush(); 168 } 169 170 public void strings(String arg) throws IOException { 171 if (arg == null) 172 throw new IllegalArgumentException ("arguments cannot be null"); 173 if (patterns != null) 174 throw new IllegalArgumentException ("rules cannot be used with strings"); 175 new StringDumper().run(arg, new PrintWriter(System.out)); 176 } 177 178 public void run(String from, String to) throws IOException { 179 if (from == null || to == null) 180 throw new IllegalArgumentException ("arguments cannot be null"); 181 if (patterns == null) 182 throw new IllegalArgumentException ("rules are required"); 183 JarProcessor proc = new MainProcessor(patterns, verbose); 184 StandaloneJarProcessor.run(new File(from), new File(to), proc); 185 } 186 } 187 | Popular Tags |