From: <cr...@us...> - 2008-11-22 03:25:49
|
Revision: 4712 http://jnode.svn.sourceforge.net/jnode/?rev=4712&view=rev Author: crawley Date: 2008-11-22 03:25:44 +0000 (Sat, 22 Nov 2008) Log Message: ----------- Refine Argument / FileArgument with EXISTING / NONEXISTENT flags. Modified Paths: -------------- trunk/fs/src/fs/org/jnode/fs/command/CatCommand.java trunk/fs/src/fs/org/jnode/fs/command/CdCommand.java trunk/fs/src/fs/org/jnode/fs/command/CpCommand.java trunk/fs/src/fs/org/jnode/fs/command/DFCommand.java trunk/fs/src/fs/org/jnode/fs/command/DeleteCommand.java trunk/fs/src/fs/org/jnode/fs/command/DirCommand.java trunk/fs/src/fs/org/jnode/fs/command/EjectCommand.java trunk/fs/src/fs/org/jnode/fs/command/HexdumpCommand.java trunk/fs/src/fs/org/jnode/fs/command/Md5SumCommand.java trunk/fs/src/fs/org/jnode/fs/command/MkdirCommand.java trunk/shell/src/shell/org/jnode/shell/syntax/Argument.java trunk/shell/src/shell/org/jnode/shell/syntax/FileArgument.java Modified: trunk/fs/src/fs/org/jnode/fs/command/CatCommand.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/command/CatCommand.java 2008-11-21 21:37:51 UTC (rev 4711) +++ trunk/fs/src/fs/org/jnode/fs/command/CatCommand.java 2008-11-22 03:25:44 UTC (rev 4712) @@ -52,11 +52,11 @@ public class CatCommand extends AbstractCommand { private final FileArgument ARG_FILE = - new FileArgument("file", Argument.OPTIONAL | Argument.MULTIPLE, + new FileArgument("file", Argument.OPTIONAL | Argument.MULTIPLE | Argument.EXISTING, "the files to be concatenated"); private final URLArgument ARG_URL = - new URLArgument("url", Argument.OPTIONAL | Argument.MULTIPLE, + new URLArgument("url", Argument.OPTIONAL | Argument.MULTIPLE | Argument.EXISTING, "the urls to be concatenated"); private final FlagArgument FLAG_URLS = Modified: trunk/fs/src/fs/org/jnode/fs/command/CdCommand.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/command/CdCommand.java 2008-11-21 21:37:51 UTC (rev 4711) +++ trunk/fs/src/fs/org/jnode/fs/command/CdCommand.java 2008-11-22 03:25:44 UTC (rev 4712) @@ -39,7 +39,7 @@ public class CdCommand extends AbstractCommand { private final FileArgument ARG_DIR = new FileArgument( - "directory", Argument.OPTIONAL, "the directory to change to"); + "directory", Argument.OPTIONAL | Argument.EXISTING, "the directory to change to"); public CdCommand() { super("Change the current directory"); Modified: trunk/fs/src/fs/org/jnode/fs/command/CpCommand.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/command/CpCommand.java 2008-11-21 21:37:51 UTC (rev 4711) +++ trunk/fs/src/fs/org/jnode/fs/command/CpCommand.java 2008-11-22 03:25:44 UTC (rev 4712) @@ -50,7 +50,8 @@ static final byte MODE_UPDATE = 3; private final FileArgument ARG_SOURCE = - new FileArgument("source", Argument.MANDATORY + Argument.MULTIPLE, "source files or directories"); + new FileArgument("source", Argument.MANDATORY | Argument.MULTIPLE | Argument.EXISTING, + "source files or directories"); private final FileArgument ARG_TARGET = new FileArgument("target", Argument.MANDATORY, "target file or directory"); Modified: trunk/fs/src/fs/org/jnode/fs/command/DFCommand.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/command/DFCommand.java 2008-11-21 21:37:51 UTC (rev 4711) +++ trunk/fs/src/fs/org/jnode/fs/command/DFCommand.java 2008-11-22 03:25:44 UTC (rev 4712) @@ -45,7 +45,7 @@ public class DFCommand extends AbstractCommand { private final DeviceArgument ARG_DEVICE = new DeviceArgument( - "device", Argument.OPTIONAL, + "device", Argument.OPTIONAL | Argument.EXISTING, "The device for which disk usage inforrmation should be displayed"); public DFCommand() { Modified: trunk/fs/src/fs/org/jnode/fs/command/DeleteCommand.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/command/DeleteCommand.java 2008-11-21 21:37:51 UTC (rev 4711) +++ trunk/fs/src/fs/org/jnode/fs/command/DeleteCommand.java 2008-11-22 03:25:44 UTC (rev 4712) @@ -45,7 +45,7 @@ public class DeleteCommand extends AbstractCommand { private final FileArgument ARG_PATHS = new FileArgument( - "paths", Argument.MANDATORY | Argument.MULTIPLE, + "paths", Argument.MANDATORY | Argument.MULTIPLE | Argument.EXISTING, "the files or directories to be deleted"); private final FlagArgument FLAG_RECURSIVE = new FlagArgument( "recursive", Argument.OPTIONAL, Modified: trunk/fs/src/fs/org/jnode/fs/command/DirCommand.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/command/DirCommand.java 2008-11-21 21:37:51 UTC (rev 4711) +++ trunk/fs/src/fs/org/jnode/fs/command/DirCommand.java 2008-11-22 03:25:44 UTC (rev 4712) @@ -45,7 +45,8 @@ private static final SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm"); private final FileArgument ARG_PATH = new FileArgument( - "path", Argument.OPTIONAL + Argument.MULTIPLE, "the file or directory to list"); + "path", Argument.OPTIONAL | Argument.MULTIPLE | Argument.EXISTING, + "the file or directory to list"); public DirCommand() { super("List files or directories"); Modified: trunk/fs/src/fs/org/jnode/fs/command/EjectCommand.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/command/EjectCommand.java 2008-11-21 21:37:51 UTC (rev 4711) +++ trunk/fs/src/fs/org/jnode/fs/command/EjectCommand.java 2008-11-22 03:25:44 UTC (rev 4712) @@ -37,7 +37,7 @@ public class EjectCommand extends AbstractCommand { private final DeviceArgument ARG_DEVICE = new DeviceArgument( - "device", Argument.MANDATORY, "device to eject the medium from", + "device", Argument.MANDATORY | Argument.EXISTING, "device to eject the medium from", RemovableDeviceAPI.class); public EjectCommand() { Modified: trunk/fs/src/fs/org/jnode/fs/command/HexdumpCommand.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/command/HexdumpCommand.java 2008-11-21 21:37:51 UTC (rev 4711) +++ trunk/fs/src/fs/org/jnode/fs/command/HexdumpCommand.java 2008-11-22 03:25:44 UTC (rev 4712) @@ -24,10 +24,10 @@ */ public class HexdumpCommand extends AbstractCommand { private final FileArgument ARG_FILE = new FileArgument( - "file", Argument.OPTIONAL, "the file to print out"); + "file", Argument.OPTIONAL | Argument.EXISTING, "the file to print out"); private final URLArgument ARG_URL = new URLArgument( - "url", Argument.OPTIONAL, "the url to print out"); + "url", Argument.OPTIONAL | Argument.EXISTING, "the url to print out"); public HexdumpCommand() { super("Print a hexadecimal dump of a given file (or URL)"); Modified: trunk/fs/src/fs/org/jnode/fs/command/Md5SumCommand.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/command/Md5SumCommand.java 2008-11-21 21:37:51 UTC (rev 4711) +++ trunk/fs/src/fs/org/jnode/fs/command/Md5SumCommand.java 2008-11-22 03:25:44 UTC (rev 4712) @@ -50,13 +50,13 @@ public class Md5SumCommand extends AbstractCommand { private final FileArgument ARG_PATHS = new FileArgument( - "paths", Argument.OPTIONAL | Argument.MULTIPLE, + "paths", Argument.OPTIONAL | Argument.MULTIPLE | Argument.EXISTING, "the files (or directories) to be calculate MD5 digests for"); private final FlagArgument FLAG_RECURSIVE = new FlagArgument( "recursive", Argument.OPTIONAL, "if set, recursively calculate MD5 digests for the contents of any directory"); private final FileArgument ARG_CHECKFILE = new FileArgument( - "checkfile", Argument.OPTIONAL | Argument.SINGLE, + "checkfile", Argument.OPTIONAL | Argument.SINGLE | Argument.EXISTING, "check MD5 digests for files listed in this file"); Modified: trunk/fs/src/fs/org/jnode/fs/command/MkdirCommand.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/command/MkdirCommand.java 2008-11-21 21:37:51 UTC (rev 4711) +++ trunk/fs/src/fs/org/jnode/fs/command/MkdirCommand.java 2008-11-22 03:25:44 UTC (rev 4712) @@ -35,7 +35,7 @@ public class MkdirCommand extends AbstractCommand { private final FileArgument ARG_DIR = new FileArgument( - "directory", Argument.MANDATORY, "the directory to create"); + "directory", Argument.MANDATORY | Argument.NONEXISTENT, "the directory to create"); public MkdirCommand() { super("Create a new directory"); Modified: trunk/shell/src/shell/org/jnode/shell/syntax/Argument.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/syntax/Argument.java 2008-11-21 21:37:51 UTC (rev 4711) +++ trunk/shell/src/shell/org/jnode/shell/syntax/Argument.java 2008-11-22 03:25:44 UTC (rev 4712) @@ -55,15 +55,45 @@ */ public abstract class Argument<V> { - public static final int OPTIONAL = 0x00; - public static final int MANDATORY = 0x01; + /** + * This Argument flag indicates that the Argument is optional. + */ + public static final int OPTIONAL = 0x001; + + /** + * This Argument flag indicates that the Argument is mandatory. At least + * one instance of this Argument must be supplied. + */ + public static final int MANDATORY = 0x002; - public static final int SINGLE = 0x00; - public static final int MULTIPLE = 0x04; + /** + * This Argument flag indicates that the Argument may have at most one value. + */ + public static final int SINGLE = 0x004; + /** + * This Argument flag indicates that multiple instances of this Argument may + * be provided. + */ + public static final int MULTIPLE = 0x008; + + /** + * This Argument flag indicates that an Argument's value must denote an entity + * that already exists in whatever domain that the Argument values corresponds to. + */ + public static final int EXISTING = 0x010; + + /** + * This Argument flag indicates that an Argument's value must denote an entity + * that does not exists in whatever domain that the Argument values corresponds to. + */ + public static final int NONEXISTENT = 0x020; + private final String label; private final boolean mandatory; private final boolean multiple; + private final boolean existing; + private final boolean nonexistent; private final String description; protected final List<V> values = new ArrayList<V>(); @@ -80,22 +110,36 @@ * @param vArray A template array used by the getValues method. It is * typically zero length. * @param description Optional documentation for the argument. + * @throws IllegalArgumentException if the flags are inconsistent */ - protected Argument(String label, int flags, V[] vArray, String description) { + protected Argument(String label, int flags, V[] vArray, String description) + throws IllegalArgumentException { super(); + if ((flags & EXISTING) != 0 && (flags & NONEXISTENT) != 0) { + throw new IllegalArgumentException("inconsistent flags: EXISTING and NONEXISTENT"); + } + if ((flags & SINGLE) != 0 && (flags & MULTIPLE) != 0) { + throw new IllegalArgumentException("inconsistent flags: SINGLE and MULTIPLE"); + } + if ((flags & MANDATORY) != 0 && (flags & OPTIONAL) != 0) { + throw new IllegalArgumentException("inconsistent flags: MANDATORY and OPTIONAL"); + } this.label = label; this.description = description; this.mandatory = (flags & MANDATORY) != 0; this.multiple = (flags & MULTIPLE) != 0; + this.existing = (flags & EXISTING) != 0; + this.nonexistent = (flags & NONEXISTENT) != 0; this.vArray = vArray; } /** - * Reconstruct and return this Arguments flags as passed to the constructor. + * Reconstruct and return Argument flags equiivalent to those passed to the constructor. * @return the flags. */ public int getFlags() { - return (mandatory ? MANDATORY : 0) | (multiple ? MULTIPLE : 0); + return ((mandatory ? MANDATORY : OPTIONAL) | (multiple ? MULTIPLE : SINGLE) | + (existing ? EXISTING : 0) | (nonexistent ? NONEXISTENT : 0)); } /** @@ -212,6 +256,22 @@ return multiple; } + /** + * If this method returns <code>true</code>, an Argument value must correspond + * to an existing entity in the domain of entities denoted by the Argument type. + */ + public boolean isExisting() { + return existing; + } + + /** + * If this method returns <code>true</code>, an Argument value must <i>not</i> correspond + * to an existing entity in the domain of entities denoted by the Argument type. + */ + public boolean isNonexistent() { + return nonexistent; + } + void setBundle(ArgumentBundle bundle) { this.bundle = bundle; } Modified: trunk/shell/src/shell/org/jnode/shell/syntax/FileArgument.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/syntax/FileArgument.java 2008-11-21 21:37:51 UTC (rev 4711) +++ trunk/shell/src/shell/org/jnode/shell/syntax/FileArgument.java 2008-11-22 03:25:44 UTC (rev 4712) @@ -29,7 +29,9 @@ import org.jnode.shell.CommandLine.Token; /** - * This argument class performs completion against the file system namespace. + * This argument class performs completion against the file system namespace. This + * Argument class understands the {@link Argument#EXISTING} and {@link Argument#NONEXISTENT} + * flags when accepting argument values, but not (yet) when completing them. * * @author cr...@jn... */ @@ -45,9 +47,15 @@ @Override protected File doAccept(Token token) throws CommandSyntaxException { - // FIXME ... do proper filename checks ... if (token.token.length() > 0) { - return new File(token.token); + File file = new File(token.token); + if (isExisting() && !file.exists()) { + throw new CommandSyntaxException("this file or directory does not exist"); + } + if (isNonexistent() && file.exists()) { + throw new CommandSyntaxException("this file or directory already exist"); + } + return file; } else { throw new CommandSyntaxException("invalid file name"); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |