1 |
779 |
jeremybenn |
/* Main.java - jar program main()
|
2 |
|
|
Copyright (C) 2006, 2007 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package gnu.classpath.tools.jar;
|
40 |
|
|
|
41 |
|
|
import gnu.classpath.tools.common.ClasspathToolParser;
|
42 |
|
|
import gnu.classpath.tools.getopt.FileArgumentCallback;
|
43 |
|
|
import gnu.classpath.tools.getopt.Option;
|
44 |
|
|
import gnu.classpath.tools.getopt.OptionException;
|
45 |
|
|
import gnu.classpath.tools.getopt.OptionGroup;
|
46 |
|
|
import gnu.classpath.tools.getopt.Parser;
|
47 |
|
|
|
48 |
|
|
import java.io.BufferedReader;
|
49 |
|
|
import java.io.File;
|
50 |
|
|
import java.io.InputStreamReader;
|
51 |
|
|
import java.io.IOException;
|
52 |
|
|
import java.text.MessageFormat;
|
53 |
|
|
import java.util.ArrayList;
|
54 |
|
|
import java.util.zip.ZipOutputStream;
|
55 |
|
|
|
56 |
|
|
public class Main
|
57 |
|
|
{
|
58 |
|
|
/** The mode of operation. This is the class representing
|
59 |
|
|
* the action; we make a new instance before using it. It
|
60 |
|
|
* must be a subclass of Action. 'null' means the mode
|
61 |
|
|
* has not yet been set. */
|
62 |
|
|
Class operationMode;
|
63 |
|
|
|
64 |
|
|
/** The archive file name. */
|
65 |
|
|
File archiveFile;
|
66 |
|
|
|
67 |
|
|
/** The zip storage mode. */
|
68 |
|
|
int storageMode = ZipOutputStream.DEFLATED;
|
69 |
|
|
|
70 |
|
|
/** True if we should read file names from stdin. */
|
71 |
|
|
boolean readNamesFromStdin = false;
|
72 |
|
|
|
73 |
|
|
/** True for verbose mode. */
|
74 |
|
|
boolean verbose = false;
|
75 |
|
|
|
76 |
|
|
/** True if we want a manifest file. */
|
77 |
|
|
boolean wantManifest = true;
|
78 |
|
|
|
79 |
|
|
/** Name of manifest file to use. */
|
80 |
|
|
File manifestFile;
|
81 |
|
|
|
82 |
|
|
/** A list of Entry objects, each describing a file to write. */
|
83 |
|
|
ArrayList<Entry> entries = new ArrayList<Entry>();
|
84 |
|
|
|
85 |
|
|
/** Used only while parsing, holds the first argument for -C. */
|
86 |
|
|
String changedDirectory;
|
87 |
|
|
|
88 |
|
|
void setArchiveFile(String filename) throws OptionException
|
89 |
|
|
{
|
90 |
|
|
if (archiveFile != null)
|
91 |
|
|
{
|
92 |
|
|
String fmt = MessageFormat.format(Messages.getString("Main.ArchiveAlreadySet"), //$NON-NLS-1$
|
93 |
|
|
new Object[] { archiveFile });
|
94 |
|
|
throw new OptionException(fmt);
|
95 |
|
|
}
|
96 |
|
|
archiveFile = new File(filename);
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
class HandleFile
|
100 |
|
|
extends FileArgumentCallback
|
101 |
|
|
{
|
102 |
|
|
public void notifyFile(String fileArgument)
|
103 |
|
|
{
|
104 |
|
|
Entry entry;
|
105 |
|
|
if (changedDirectory != null)
|
106 |
|
|
{
|
107 |
|
|
entry = new Entry(new File(changedDirectory, fileArgument),
|
108 |
|
|
fileArgument);
|
109 |
|
|
changedDirectory = null;
|
110 |
|
|
}
|
111 |
|
|
else
|
112 |
|
|
entry = new Entry(new File(fileArgument));
|
113 |
|
|
entries.add(entry);
|
114 |
|
|
}
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
// An option that knows how to set the operation mode.
|
118 |
|
|
private class ModeOption
|
119 |
|
|
extends Option
|
120 |
|
|
{
|
121 |
|
|
private Class mode;
|
122 |
|
|
|
123 |
|
|
public ModeOption(char shortName, String description, Class mode)
|
124 |
|
|
{
|
125 |
|
|
super(shortName, description);
|
126 |
|
|
this.mode = mode;
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
public ModeOption(char shortName, String description, String argName,
|
130 |
|
|
Class mode)
|
131 |
|
|
{
|
132 |
|
|
super(shortName, description, argName);
|
133 |
|
|
this.mode = mode;
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
public void parsed(String argument) throws OptionException
|
137 |
|
|
{
|
138 |
|
|
if (operationMode != null)
|
139 |
|
|
throw new OptionException(Messages.getString("Main.ModeAlreaySet")); //$NON-NLS-1$
|
140 |
|
|
operationMode = mode;
|
141 |
|
|
// We know this is only the case for -i.
|
142 |
|
|
if (argument != null)
|
143 |
|
|
setArchiveFile(argument);
|
144 |
|
|
}
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
private class JarParser extends ClasspathToolParser
|
148 |
|
|
{
|
149 |
|
|
public JarParser(String name)
|
150 |
|
|
{
|
151 |
|
|
super(name);
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
protected void validate() throws OptionException
|
155 |
|
|
{
|
156 |
|
|
if (operationMode == null)
|
157 |
|
|
throw new OptionException(Messages.getString("Main.MustSpecify")); //$NON-NLS-1$
|
158 |
|
|
if (changedDirectory != null)
|
159 |
|
|
throw new OptionException(Messages.getString("Main.TwoArgsReqd")); //$NON-NLS-1$
|
160 |
|
|
if (! wantManifest && manifestFile != null)
|
161 |
|
|
throw new OptionException(Messages.getString("Main.CantHaveBoth")); //$NON-NLS-1$
|
162 |
|
|
if (operationMode == Indexer.class)
|
163 |
|
|
{
|
164 |
|
|
// Some extra validation for -i.
|
165 |
|
|
if (! entries.isEmpty())
|
166 |
|
|
throw new OptionException(Messages.getString("Main.NoFilesWithi")); //$NON-NLS-1$
|
167 |
|
|
if (! wantManifest)
|
168 |
|
|
throw new OptionException(Messages.getString("Main.NoMAndi")); //$NON-NLS-1$
|
169 |
|
|
if (manifestFile != null)
|
170 |
|
|
throw new OptionException(Messages.getString("Main.AnotherNomAndi")); //$NON-NLS-1$
|
171 |
|
|
}
|
172 |
|
|
}
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
private ClasspathToolParser initializeParser()
|
176 |
|
|
{
|
177 |
|
|
ClasspathToolParser p = new JarParser("jar"); //$NON-NLS-1$
|
178 |
|
|
p.setHeader(Messages.getString("Main.Usage")); //$NON-NLS-1$
|
179 |
|
|
|
180 |
|
|
OptionGroup grp = new OptionGroup(Messages.getString("Main.OpMode")); //$NON-NLS-1$
|
181 |
|
|
grp.add(new ModeOption('c', Messages.getString("Main.Create"), Creator.class)); //$NON-NLS-1$
|
182 |
|
|
grp.add(new ModeOption('x', Messages.getString("Main.Extract"), Extractor.class)); //$NON-NLS-1$
|
183 |
|
|
grp.add(new ModeOption('t', Messages.getString("Main.List"), Lister.class)); //$NON-NLS-1$
|
184 |
|
|
grp.add(new ModeOption('u', Messages.getString("Main.Update"), Updater.class)); //$NON-NLS-1$
|
185 |
|
|
// Note that -i works in-place and explicitly requires a file name.
|
186 |
|
|
grp.add(new ModeOption('i', Messages.getString("Main.Index"), Messages.getString("Main.FileArg"), Indexer.class)); //$NON-NLS-1$ //$NON-NLS-2$
|
187 |
|
|
p.add(grp);
|
188 |
|
|
|
189 |
|
|
grp = new OptionGroup(Messages.getString("Main.OpMods")); //$NON-NLS-1$
|
190 |
|
|
grp.add(new Option('f', Messages.getString("Main.ArchiveName"), Messages.getString("Main.FileArg2")) //$NON-NLS-1$ //$NON-NLS-2$
|
191 |
|
|
{
|
192 |
|
|
public void parsed(String argument) throws OptionException
|
193 |
|
|
{
|
194 |
|
|
setArchiveFile(argument);
|
195 |
|
|
}
|
196 |
|
|
});
|
197 |
|
|
grp.add(new Option('0', Messages.getString("Main.NoZip")) //$NON-NLS-1$
|
198 |
|
|
{
|
199 |
|
|
public void parsed(String argument) throws OptionException
|
200 |
|
|
{
|
201 |
|
|
storageMode = ZipOutputStream.STORED;
|
202 |
|
|
}
|
203 |
|
|
});
|
204 |
|
|
grp.add(new Option('v', Messages.getString("Main.Verbose")) //$NON-NLS-1$
|
205 |
|
|
{
|
206 |
|
|
public void parsed(String argument) throws OptionException
|
207 |
|
|
{
|
208 |
|
|
verbose = true;
|
209 |
|
|
}
|
210 |
|
|
});
|
211 |
|
|
grp.add(new Option('M', Messages.getString("Main.NoManifest")) //$NON-NLS-1$
|
212 |
|
|
{
|
213 |
|
|
public void parsed(String argument) throws OptionException
|
214 |
|
|
{
|
215 |
|
|
wantManifest = false;
|
216 |
|
|
}
|
217 |
|
|
});
|
218 |
|
|
grp.add(new Option('m', Messages.getString("Main.ManifestName"), Messages.getString("Main.ManifestArgName")) //$NON-NLS-1$ //$NON-NLS-2$
|
219 |
|
|
{
|
220 |
|
|
public void parsed(String argument) throws OptionException
|
221 |
|
|
{
|
222 |
|
|
manifestFile = new File(argument);
|
223 |
|
|
}
|
224 |
|
|
});
|
225 |
|
|
// -@
|
226 |
|
|
p.add(grp);
|
227 |
|
|
|
228 |
|
|
grp = new OptionGroup(Messages.getString("Main.FileNameGroup")); //$NON-NLS-1$
|
229 |
|
|
grp.add(new Option('C', Messages.getString("Main.ChangeDir"), //$NON-NLS-1$
|
230 |
|
|
Messages.getString("Main.ChangeDirArg")) //$NON-NLS-1$
|
231 |
|
|
{
|
232 |
|
|
public void parsed(String argument) throws OptionException
|
233 |
|
|
{
|
234 |
|
|
changedDirectory = argument;
|
235 |
|
|
}
|
236 |
|
|
});
|
237 |
|
|
grp.add(new Option('@', Messages.getString("Main.Stdin"))
|
238 |
|
|
{
|
239 |
|
|
public void parsed(String argument) throws OptionException
|
240 |
|
|
{
|
241 |
|
|
readNamesFromStdin = true;
|
242 |
|
|
}
|
243 |
|
|
});
|
244 |
|
|
p.add(grp);
|
245 |
|
|
|
246 |
|
|
return p;
|
247 |
|
|
}
|
248 |
|
|
|
249 |
|
|
private void readNames()
|
250 |
|
|
{
|
251 |
|
|
String line;
|
252 |
|
|
try
|
253 |
|
|
{
|
254 |
|
|
BufferedReader br
|
255 |
|
|
= new BufferedReader(new InputStreamReader(System.in));
|
256 |
|
|
while ((line = br.readLine()) != null)
|
257 |
|
|
entries.add(new Entry(new File(line)));
|
258 |
|
|
}
|
259 |
|
|
catch (IOException _)
|
260 |
|
|
{
|
261 |
|
|
// Ignore.
|
262 |
|
|
}
|
263 |
|
|
}
|
264 |
|
|
|
265 |
|
|
private void run(String[] args)
|
266 |
|
|
throws InstantiationException, IllegalAccessException, IOException
|
267 |
|
|
{
|
268 |
|
|
ClasspathToolParser p = initializeParser();
|
269 |
|
|
// Special hack to emulate old tar-style commands.
|
270 |
|
|
if (args.length > 0 && args[0].charAt(0) != '-')
|
271 |
|
|
args[0] = '-' + args[0];
|
272 |
|
|
p.parse(args, new HandleFile(), true);
|
273 |
|
|
if (readNamesFromStdin)
|
274 |
|
|
readNames();
|
275 |
|
|
Action t = (Action) operationMode.newInstance();
|
276 |
|
|
t.run(this);
|
277 |
|
|
}
|
278 |
|
|
|
279 |
|
|
public static void main(String[] args)
|
280 |
|
|
{
|
281 |
|
|
Main jarprogram = new Main();
|
282 |
|
|
try
|
283 |
|
|
{
|
284 |
|
|
jarprogram.run(args);
|
285 |
|
|
}
|
286 |
|
|
catch (Exception e)
|
287 |
|
|
{
|
288 |
|
|
System.err.println(Messages.getString("Main.InternalError")); //$NON-NLS-1$
|
289 |
|
|
e.printStackTrace(System.err);
|
290 |
|
|
System.exit(1);
|
291 |
|
|
}
|
292 |
|
|
}
|
293 |
|
|
}
|