KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonicsystems > jarjar > Main


1 /*
2   Jar Jar Links - A utility to repackage and embed Java libraries
3   Copyright (C) 2004 Tonic Systems, Inc.
4
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; either version 2 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program; see the file COPYING. if not, write to
17   the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18   Boston, MA 02111-1307 USA
19 */

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 JavaDoc 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     // TODO: standalone kill?
48
public static void main(String JavaDoc[] 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 JavaDoc 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 JavaDoc("unknown level " + level);
82                     }
83                     break;
84                 case 4:
85                     String JavaDoc style = g.getOptarg();
86                     if ("simple".equals(style)) {
87                         main.setStyle(STYLE_SIMPLE);
88                     } else {
89                         throw new IllegalArgumentException JavaDoc("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 JavaDoc("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 JavaDoc 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 JavaDoc("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 JavaDoc arg) throws IOException {
156         find(arg, arg);
157     }
158     
159     public void find(String JavaDoc from, String JavaDoc to) throws IOException {
160         if (from == null || to == null)
161             throw new IllegalArgumentException JavaDoc("arguments cannot be null");
162         if (patterns != null)
163             throw new IllegalArgumentException JavaDoc("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 JavaDoc arg) throws IOException {
171         if (arg == null)
172             throw new IllegalArgumentException JavaDoc("arguments cannot be null");
173         if (patterns != null)
174             throw new IllegalArgumentException JavaDoc("rules cannot be used with strings");
175         new StringDumper().run(arg, new PrintWriter(System.out));
176     }
177
178     public void run(String JavaDoc from, String JavaDoc to) throws IOException {
179         if (from == null || to == null)
180             throw new IllegalArgumentException JavaDoc("arguments cannot be null");
181         if (patterns == null)
182             throw new IllegalArgumentException JavaDoc("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