View Javadoc

1   /**
2    * Copyright 2007 Google Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.tonicsystems.jarjar;
18  
19  import com.tonicsystems.jarjar.util.*;
20  import java.io.*;
21  import java.util.*;
22  
23  public class Main {
24  
25    private static final String LINE_SEPARATOR = System.getProperty("line.separator");
26    private static final String HELP;
27  
28    static {
29      try {
30        HELP = readIntoString(Main.class.getResourceAsStream("help.txt"));
31      } catch (IOException e) {
32        throw new RuntimeIOException(e);
33      }
34    }
35  
36    private static String readIntoString(InputStream in) throws IOException {
37        StringBuilder sb = new StringBuilder();
38        BufferedReader r = new BufferedReader(new InputStreamReader(in, "UTF-8"));
39        String line = null;
40        while ((line = r.readLine()) != null)
41            sb.append(line).append(LINE_SEPARATOR);
42        return sb.toString();
43    }
44  
45    private boolean verbose;
46    private List patterns;
47    private int level = DepHandler.LEVEL_CLASS;
48  
49    public static void main(String[] args) throws Exception {
50      MainUtil.runMain(new Main(), args, "help");
51    }
52  
53    public void help() {
54      System.err.print(HELP);
55    }
56  
57    public void strings(String cp) throws IOException {
58      if (cp == null) {
59        throw new IllegalArgumentException("cp is required");
60      }
61      new StringDumper().run(cp, new PrintWriter(System.out));
62    }
63  
64    // TODO: make level an enum
65    public void find(String level, String cp1, String cp2) throws IOException {
66      if (level == null || cp1 == null) {
67        throw new IllegalArgumentException("level and cp1 are required");
68      }
69      if (cp2 == null) {
70        cp2 = cp1;
71      }
72      int levelFlag;
73      if ("class".equals(level)) {
74        levelFlag = DepHandler.LEVEL_CLASS;
75      } else if ("jar".equals(level)) {
76        levelFlag = DepHandler.LEVEL_JAR;
77      } else {
78        throw new IllegalArgumentException("unknown level " + level);
79      }
80      PrintWriter w = new PrintWriter(System.out);
81      DepHandler handler = new TextDepHandler(w, levelFlag);
82      new DepFind().run(cp1, cp2, handler);
83      w.flush();
84    }
85  
86    public void process(File rulesFile, File inJar, File outJar) throws IOException {
87      if (rulesFile == null || inJar == null || outJar == null) {
88        throw new IllegalArgumentException("rulesFile, inJar, and outJar are required");
89      }
90      List<PatternElement> rules = RulesFileParser.parse(rulesFile);
91      boolean verbose = Boolean.getBoolean("verbose");
92      boolean skipManifest = Boolean.getBoolean("skipManifest");
93      MainProcessor proc = new MainProcessor(rules, verbose, skipManifest);
94      StandaloneJarProcessor.run(inJar, outJar, proc);
95      proc.strip(outJar);
96    }
97  }