summaryrefslogtreecommitdiff
path: root/tools/gnu/classpath/tools/jar/Main.java
diff options
context:
space:
mode:
Diffstat (limited to 'tools/gnu/classpath/tools/jar/Main.java')
-rw-r--r--tools/gnu/classpath/tools/jar/Main.java133
1 files changed, 87 insertions, 46 deletions
diff --git a/tools/gnu/classpath/tools/jar/Main.java b/tools/gnu/classpath/tools/jar/Main.java
index ee4da3c27..8ea770bb6 100644
--- a/tools/gnu/classpath/tools/jar/Main.java
+++ b/tools/gnu/classpath/tools/jar/Main.java
@@ -47,41 +47,53 @@ import gnu.classpath.tools.getopt.Parser;
import java.io.File;
import java.io.IOException;
+import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.zip.ZipOutputStream;
public class Main
{
- // The mode of operation. This is the class representing
- // the action; we make a new instance before using it. It
- // must be a subclass of Action. 'null' means the mode
- // has not yet been set.
+ /** The mode of operation. This is the class representing
+ * the action; we make a new instance before using it. It
+ * must be a subclass of Action. 'null' means the mode
+ * has not yet been set. */
Class operationMode;
- // The archive file name.
+ /** The archive file name. */
File archiveFile;
- // The zip storage mode.
+ /** The zip storage mode. */
int storageMode = ZipOutputStream.DEFLATED;
- // True if we should read file names from stdin.
+ /** True if we should read file names from stdin. */
boolean readNamesFromStdin = false;
- // True for verbose mode.
+ /** True for verbose mode. */
boolean verbose = false;
- // True if we want a manifest file.
+ /** True if we want a manifest file. */
boolean wantManifest = true;
- // Name of manifest file to use.
+ /** Name of manifest file to use. */
File manifestFile;
- // A list of Entry objects, each describing a file to write.
+ /** A list of Entry objects, each describing a file to write. */
ArrayList entries = new ArrayList();
- // Used only while parsing.
+ /** Used only while parsing, holds the first argument for -C. */
String changedDirectory;
+ void setArchiveFile(String filename) throws OptionException
+ {
+ if (archiveFile != null)
+ {
+ String fmt = MessageFormat.format(Messages.getString("Main.ArchiveAlreadySet"), //$NON-NLS-1$
+ new Object[] { archiveFile });
+ throw new OptionException(fmt);
+ }
+ archiveFile = new File(filename);
+ }
+
class HandleFile
extends FileArgumentCallback
{
@@ -112,57 +124,96 @@ public class Main
this.mode = mode;
}
+ public ModeOption(char shortName, String description, String argName,
+ Class mode)
+ {
+ super(shortName, description, argName);
+ this.mode = mode;
+ }
+
public void parsed(String argument) throws OptionException
{
if (operationMode != null)
- throw new OptionException("operation mode already specified");
+ throw new OptionException(Messages.getString("Main.ModeAlreaySet")); //$NON-NLS-1$
operationMode = mode;
+ // We know this is only the case for -i.
+ if (argument != null)
+ setArchiveFile(argument);
+ }
+ }
+
+ private class JarParser extends ClasspathToolParser
+ {
+ public JarParser(String name)
+ {
+ super(name);
+ }
+
+ protected void validate() throws OptionException
+ {
+ if (operationMode == null)
+ throw new OptionException(Messages.getString("Main.MustSpecify")); //$NON-NLS-1$
+ if (changedDirectory != null)
+ throw new OptionException(Messages.getString("Main.TwoArgsReqd")); //$NON-NLS-1$
+ if (! wantManifest && manifestFile != null)
+ throw new OptionException(Messages.getString("Main.CantHaveBoth")); //$NON-NLS-1$
+ if (operationMode == Indexer.class)
+ {
+ // Some extra validation for -i.
+ if (! entries.isEmpty())
+ throw new OptionException(Messages.getString("Main.NoFilesWithi")); //$NON-NLS-1$
+ if (! wantManifest)
+ throw new OptionException(Messages.getString("Main.NoMAndi")); //$NON-NLS-1$
+ if (manifestFile != null)
+ throw new OptionException(Messages.getString("Main.AnotherNomAndi")); //$NON-NLS-1$
+ }
}
}
private Parser initializeParser()
{
- Parser p = new ClasspathToolParser("jar");
- p.setHeader("Usage: jar -ctxu [OPTIONS] jar-file [-C DIR FILE] FILE...");
-
- OptionGroup grp = new OptionGroup("Operation mode");
- grp.add(new ModeOption('c', "create a new archive", Creator.class));
- grp.add(new ModeOption('x', "extract from archive", Extractor.class));
- grp.add(new ModeOption('t', "list archive contents", Lister.class));
- grp.add(new ModeOption('u', "update archive", Updater.class));
+ Parser p = new JarParser("jar"); //$NON-NLS-1$
+ p.setHeader(Messages.getString("Main.Usage")); //$NON-NLS-1$
+
+ OptionGroup grp = new OptionGroup(Messages.getString("Main.OpMode")); //$NON-NLS-1$
+ grp.add(new ModeOption('c', Messages.getString("Main.Create"), Creator.class)); //$NON-NLS-1$
+ grp.add(new ModeOption('x', Messages.getString("Main.Extract"), Extractor.class)); //$NON-NLS-1$
+ grp.add(new ModeOption('t', Messages.getString("Main.List"), Lister.class)); //$NON-NLS-1$
+ grp.add(new ModeOption('u', Messages.getString("Main.Update"), Updater.class)); //$NON-NLS-1$
+ // Note that -i works in-place and explicitly requires a file name.
+ grp.add(new ModeOption('i', Messages.getString("Main.Index"), Messages.getString("Main.FileArg"), Indexer.class)); //$NON-NLS-1$ //$NON-NLS-2$
p.add(grp);
- grp = new OptionGroup("Operation modifiers");
- grp.add(new Option('f', "specify archive file name", "FILE")
+ grp = new OptionGroup(Messages.getString("Main.OpMods")); //$NON-NLS-1$
+ grp.add(new Option('f', Messages.getString("Main.ArchiveName"), Messages.getString("Main.FileArg2")) //$NON-NLS-1$ //$NON-NLS-2$
{
public void parsed(String argument) throws OptionException
{
- // FIXME: error if already set.
- archiveFile = new File(argument);
+ setArchiveFile(argument);
}
});
- grp.add(new Option('0', "store only; no ZIP compression")
+ grp.add(new Option('0', Messages.getString("Main.NoZip")) //$NON-NLS-1$
{
public void parsed(String argument) throws OptionException
{
storageMode = ZipOutputStream.STORED;
}
});
- grp.add(new Option('v', "verbose operation")
+ grp.add(new Option('v', Messages.getString("Main.Verbose")) //$NON-NLS-1$
{
public void parsed(String argument) throws OptionException
{
verbose = true;
}
});
- grp.add(new Option('M', "do not create a manifest file")
+ grp.add(new Option('M', Messages.getString("Main.NoManifest")) //$NON-NLS-1$
{
public void parsed(String argument) throws OptionException
{
wantManifest = false;
}
});
- grp.add(new Option('m', "specify manifest file", "FILE")
+ grp.add(new Option('m', Messages.getString("Main.ManifestName"), Messages.getString("Main.ManifestArgName")) //$NON-NLS-1$ //$NON-NLS-2$
{
public void parsed(String argument) throws OptionException
{
@@ -172,9 +223,9 @@ public class Main
// -@
p.add(grp);
- grp = new OptionGroup("File name selection");
- grp.add(new Option('C', "change to directory before the next file",
- "DIR FILE")
+ grp = new OptionGroup(Messages.getString("Main.FileNameGroup")); //$NON-NLS-1$
+ grp.add(new Option('C', Messages.getString("Main.ChangeDir"), //$NON-NLS-1$
+ Messages.getString("Main.ChangeDirArg")) //$NON-NLS-1$
{
public void parsed(String argument) throws OptionException
{
@@ -182,23 +233,18 @@ public class Main
}
});
p.add(grp);
- // -i - need to parse classes
return p;
}
- private void run(String[] args) throws OptionException,
- InstantiationException, IllegalAccessException, IOException
+ private void run(String[] args)
+ throws InstantiationException, IllegalAccessException, IOException
{
Parser p = initializeParser();
// Special hack to emulate old tar-style commands.
- if (args[0].charAt(0) != '-')
+ if (args.length > 0 && args[0].charAt(0) != '-')
args[0] = '-' + args[0];
p.parse(args, new HandleFile());
- if (operationMode == null)
- throw new OptionException("must specify one of -t, -c, -u, or -x");
- if (changedDirectory != null)
- throw new OptionException("-C argument requires both directory and filename");
Action t = (Action) operationMode.newInstance();
t.run(this);
}
@@ -210,14 +256,9 @@ public class Main
{
jarprogram.run(args);
}
- catch (OptionException arg)
- {
- System.err.println("jar: " + arg.getMessage());
- System.exit(1);
- }
catch (Exception e)
{
- System.err.println("jar: internal error:");
+ System.err.println(Messages.getString("Main.InternalError")); //$NON-NLS-1$
e.printStackTrace(System.err);
System.exit(1);
}