jpl.mipl.mdms.FileService.komodo.client
Class UtilCmdParser

java.lang.Object
  extended by jpl.mipl.mdms.FileService.komodo.client.UtilCmdParser

public class UtilCmdParser
extends Object

Purpose: Parsing utility for the UtilClient command-line arguments. This class takes an array of Strings and parses according to a set of general and specific rules. Some operations utilize options files which specify a set of arguments per line. To allow iteration through these separate invocations, clients can call iterations(),hasNext() and advance() methods.

    Copyright 2005, California Institute of Technology. ALL RIGHTS RESERVED. U.S.
    Government Sponsorship acknowledge. 2005.
    
 ============================================================================
 Modification History :  
 ----------------------
 
 Date              Who          What 
 ----------------------------------------------------------------------------
 12/16/2004        Nick          Initial Release
 05/26/2005        Nick          Added push,pull options
 09/16/2005        Nick          Added push,pull to notify
 03/09/2006        Nick          Added 'format' to add, checkfiles, get, 
                                 list, notify, replace, subscribe.
 11/10/2006        Nick          Added invokeasync option and checks
 ============================================================================
 

Version:
$Id: UtilCmdParser.java,v 1.46 2008/04/21 22:58:05 ntt Exp $
Author:
Nicholas Toole (Nicholas.T.Toole@jpl.nasa.gov)

Field Summary
protected  int _actionId
          Numeric id representing the action/operation
protected  boolean _actionIsAuto
          Flag that is true iff actionId is of type notify/subscription
protected  String _actionName
          String name representing the action/operation
protected  Hashtable _actionTable
          Table containing mapping from actionId to allowable keywords
protected  String[] _args
          Reference to the command line argument array
protected  List _argumentList
          List of arguments from options file.
protected  Hashtable _cmdLineArguments
          Command line arguments
protected  Hashtable _currentArguments
          Interval reference to the current argument hash
protected  int _currentIndex
           
protected  boolean _error
          Flag indicating error occurred during parsing
protected  Matcher _matcher
           
protected  String _optionsFile
          Name of the options file, null if none specified
protected  String[][] _optionsFileArgs
          Contains argument array for each line entry of options file
protected  Pattern _printablePattern
           
protected  String _printableRegex
           
protected  boolean _printHelp
          Flag indicating whether or not help should be printed
protected  boolean _using
          Flag indicating usage of options file
static int CODE_MISSING_ARGUMENTS
           
protected static String ERR_MSG_MISSING_ARGS
           
protected static String ERROR_TAG
           
static String KEYWORD_FILES
          Keyword used for files lookup
protected static int MAX_FILENAME_LENGTH
           
 
Constructor Summary
UtilCmdParser()
          Constructor.
 
Method Summary
 void advance()
          Advances to the next argument set.
protected  String[] buildArgs(String line)
          Method to parse a line read in from the option file to create a tokenized array of strings.
protected  void checkAllowableArguments()
           
protected  void checkConsistency(Hashtable arguments, boolean fromFile)
          This method checks an option set for known conflicting options.
protected  void generalParse()
          This method performs a general parse of the command line arguments, performs a consistency check, then applies more specific parsing according to the action id.
 Map getCurrentArguments()
          Returns reference to the current argument map.
static Date getDate(String time, boolean pm)
          Translates from string representation of time to Date object.
 String getOptionsFilename()
          Returns the filename of the options file if specified at the command line argument set.
 Object getValue(String keyword)
          Returns value associated with keyword from current argument set.
 boolean hasNext()
          Returns true iff there are more iterations of operation invocations that have yet to be processed.
 boolean hasValue(String keyword)
          Returns true if value associated with keyword from current argument set is defined and non-null.
protected  void init()
          Initializes internal lookup table.
 boolean isSet(String keyword)
          Returns true if keyword was set in the current argument set.
 int iterations()
          Returns the number of invocations of the operation.
protected  void mergeArguments()
          Adds command line arguments to options file arguments, as a way of overriding and supplementing arguments.
protected  void parse()
          Given a command id and argument array, parses the arguments according to the command throwing Exception if (1) unrecognized argument (2) missing value (3) missing required argument (4) ...
 void parse(int actionId, String[] args)
          Performs the parsing of the arguments.
protected  void parseAccept()
          Parsing for fei5accept fei5accept <filetype> for <add|replace|get|delete> {[output <path>] [crc] [restart] [saferead] [autodelete] [replace|version]}
protected  void parseAdd()
          Parsing for fei5add fei5add [ <server group>:] <file type> <file name expression>...
protected  boolean parseArgs(String[] args, Hashtable argTable)
          Parse the command line arguments.
protected  void parseCheck()
          Parsing for fei5check fei5check
protected  void parseCheckfiles()
          Parsing for fei5checkfiles fei5checkfiles [ <server group>:] <file type> ["<file name expression>"] {[before|after <date-time>] | [between <date-time1>and <date-time2>] [long | verylong] [help]} fei5checkfiles using <option file> Option File Format (per line): [ <server group>:] <file type> " <file name expression>" {[before|after <date-time>] | [between <date-time1>and <date-time2>] [long | verylong]}
protected  void parseComment()
          Parsing for fei5comment fei5comment [ <server group>:] <file type> <file name> comment " <comment text>" [help]
protected  void parseCrc()
          Parsing for fei5crc fei5crc <file name expression>
protected  void parseDelete()
          Parsing for fei5delete fei5delete [ <server group>:] <file type> " <file name expression>" [help] fei5delete using <option file> Option File Format (per line): [ <server group>:] <file type> <file name>
protected  void parseDisplay()
          Parsing for fei5display fei5display [ <server group>:] <file type> <file name> [help]
protected  void parseGet()
          Parsing for fei5get fei5get [ <server group>:] <file type> [" <file name expression>"] {[output <path>] [before|after <datetime>] | [between <datetime1>and <datetime2>] [crc] [saferead] [receipt] [replace|version] [help]} fei5get using <option file> Option File Format (per line): [ <server group>:] <file type> [" <file name expression>"] {[output <path>] [before|after <date-time>] | [between <date-time1>and <date-time2>] [crc] [saferead] [receipt] [replace|version]}
protected  void parseKdestroy()
          Parsing for fei5kdestroy fei5kdestroy
protected  void parseKinit()
          Parsing for fei5kinit fei5kinit
protected  void parseKlist()
          Parsing for fei5klist fei5klist
protected  void parseList()
          Parsing for fei5list fei5list [ <server group>:] <file type> [" <file name expression>"] {[before|after <date-time>] | [between <date-time1>and <date-time2>] [long | verylong] [help]}
protected  void parseMakeclean()
          Parsing for fei5makeclean fei5makeclean [ <server group>:] <file type> "<file name expression>" [help]
protected  void parseMakeClean()
           
protected  void parseNotify()
          Parsing for fei5notify fei5notify [ <server group:] <file type> {[output <path>] [restart] [using <option file>]} Option File Format: invoke <command> invokeExitOnError invokeAsync logFile <file name> mailMessageFrom <email address> mailMessageTo <email address, email address, ...> mailReportAt <hh:mm am|pm, hh:mm am|pm, ...> mailReportTo <email address, email address, ...> mailSMTPHost <host name>
protected  void parseOptionsFile(String optionsFilename)
          Parses entries from an options file and populates the fileArguments list.
protected  void parseReference()
          Parsing for fei5reference fei5reference [ <server group>:] <file type> <file name> vft <VFT name> reference <ref name> [help]
protected  void parseRegister()
          Parsing for fei5register: fei5register [ <server group>:] <file type> <file name expression>...
protected  void parseRename()
          Parsing for fei5rename fei5rename [ <server group>:] <file type> <old file name> <new file name> [help] fei5rename using <option file> Option File Format (per line): [ <server group>:] <file type> <old file name> <new file name>
protected  void parseReplace()
          Parsing for fei5replace fei5replace [ <server group>:] <file type> <file name expression>...
protected  void parseShowtypes()
          Parsing for fei5showtypes
protected  void parseSpecific()
          Wrapper method that dispatches specific parse checking based on action id.
protected  void parseSubscribe()
          Parsing for fei5subscribe fei5subscribe [ <server group:] <file type> {[output <path>] [restart] [using <option file>]} Option File Format: crc invoke <command>invokeExitOnError logFile <file name> mailMessageFrom <email address> mailMessageTo <email address, email address, ...> mailReportAt <hh:mm am|pm, hh:mm am|pm, ...> mailReportTo <email address, email address, ...> mailSMTPHost <host name> receipt replace saferead version
static Date[] parseTimeList(String timelist)
          Returns an array of Date objects created from the timelist parameter.
protected  void parseUnregister()
          Parsing for fei5unregister: fei5unregister [ <server group>:] <file type> " <file name expression>" [help] fei5unregister using <option file> Option File Format (per line): [ <server group>:] <file type> <file name>
 boolean printHelp()
          Returns true iff help should be printed for the operation, either if help keyword was found or parse error occurred.
 void reset()
          Resets to beginning of arguments list.
protected  int setError(int lineNumber)
          Helper method that sets the error flag and returns parameter value back.
protected  boolean shouldPrintHelp()
          Protected method that determines if help should be printed based on a set of known-criteria such as error conditions.
 String toString()
          Override of toString method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

KEYWORD_FILES

public static final String KEYWORD_FILES
Keyword used for files lookup

See Also:
Constant Field Values

ERROR_TAG

protected static final String ERROR_TAG
See Also:
Constant Field Values

ERR_MSG_MISSING_ARGS

protected static final String ERR_MSG_MISSING_ARGS
See Also:
Constant Field Values

CODE_MISSING_ARGUMENTS

public static final int CODE_MISSING_ARGUMENTS
See Also:
Constant Field Values

MAX_FILENAME_LENGTH

protected static final int MAX_FILENAME_LENGTH
See Also:
Constant Field Values

_cmdLineArguments

protected Hashtable _cmdLineArguments
Command line arguments


_argumentList

protected List _argumentList
List of arguments from options file. Each entry is a hashtable of options from a line of the file.


_actionName

protected String _actionName
String name representing the action/operation


_actionId

protected int _actionId
Numeric id representing the action/operation


_args

protected String[] _args
Reference to the command line argument array


_actionTable

protected Hashtable _actionTable
Table containing mapping from actionId to allowable keywords


_actionIsAuto

protected boolean _actionIsAuto
Flag that is true iff actionId is of type notify/subscription


_currentArguments

protected Hashtable _currentArguments
Interval reference to the current argument hash


_currentIndex

protected int _currentIndex

_using

protected boolean _using
Flag indicating usage of options file


_optionsFile

protected String _optionsFile
Name of the options file, null if none specified


_printHelp

protected boolean _printHelp
Flag indicating whether or not help should be printed


_error

protected boolean _error
Flag indicating error occurred during parsing


_optionsFileArgs

protected String[][] _optionsFileArgs
Contains argument array for each line entry of options file


_printableRegex

protected final String _printableRegex
See Also:
Constant Field Values

_printablePattern

protected Pattern _printablePattern

_matcher

protected Matcher _matcher
Constructor Detail

UtilCmdParser

public UtilCmdParser()
Constructor.

Method Detail

init

protected void init()
Initializes internal lookup table.


parse

public void parse(int actionId,
                  String[] args)
           throws ParseException
Performs the parsing of the arguments.

Parameters:
actionId - Numeric id associated with the operation to be performed.
args[] - String array of arguments
Throws:
ParseException - if parsing cannot complete successfully

parse

protected void parse()
              throws ParseException
Given a command id and argument array, parses the arguments according to the command throwing Exception if (1) unrecognized argument (2) missing value (3) missing required argument (4) ...

Throws:
ParseException - if error occurs druring parsing

isSet

public boolean isSet(String keyword)
Returns true if keyword was set in the current argument set. Use hasValue() to check value of keyword.

Parameters:
keyword - Keyword of the value to be checked
Returns:
True if keyword is found in current argument set.

hasValue

public boolean hasValue(String keyword)
Returns true if value associated with keyword from current argument set is defined and non-null.

Parameters:
keyword - Keyword of the value to be checked
Returns:
True if value associated with keyword is non-null, else false.

getValue

public Object getValue(String keyword)
Returns value associated with keyword from current argument set.

Parameters:
keyword - Keyword of the value to be returned
Returns:
Value associated with keyword if found, else null.

hasNext

public boolean hasNext()
Returns true iff there are more iterations of operation invocations that have yet to be processed.

Returns:
True if next() can be called again.

advance

public void advance()
Advances to the next argument set. Should be called when return value from iterations() is greater than 1.


reset

public void reset()
Resets to beginning of arguments list.


getCurrentArguments

public Map getCurrentArguments()
Returns reference to the current argument map.

Returns:
Current argument map

iterations

public int iterations()
Returns the number of invocations of the operation. This will return a number greater than 1 if using a options list that contains one invocation per line.

Returns:
Number of expected invocations of operation based on separate argument sets per invocation.

getOptionsFilename

public String getOptionsFilename()
Returns the filename of the options file if specified at the command line argument set. If no filename was specified, null is returned.

Returns:
Options filename, null if not specified.

printHelp

public boolean printHelp()
Returns true iff help should be printed for the operation, either if help keyword was found or parse error occurred.

Returns:
True if help/usage should be printed, false otherwise.

checkConsistency

protected void checkConsistency(Hashtable arguments,
                                boolean fromFile)
                         throws ParseException
This method checks an option set for known conflicting options. (1) TimeFilterSet = {BEFORE, AFTER, BETWEEN} (2) VerboseSet = {LONG, VERYLONG} (3) DuplicateSet = {REPLACE, VERSION} (4) UsageSet = {USING, HELP} (5) Between = Looks at uses of BETWEEN and pairs with AND (6) Using = Looks at placement of USING and checks file. Verifies that action can have USING, and in most cases, if it does, has no other args (7) MailSet = checks that mail arguments are used consistently. These include {mailMessageFrom, mailMessageTo, mailReportAt, mailReportTo, mailSMTPHost}

Parameters:
args - Current hashtable of arguments being checked
Throws:
ParseException

generalParse

protected void generalParse()
                     throws ParseException
This method performs a general parse of the command line arguments, performs a consistency check, then applies more specific parsing according to the action id. If an options file was specified, it will be loaded and then finally operation-specific parsing will be applied.

Throws:
ParseException

checkAllowableArguments

protected void checkAllowableArguments()
                                throws ParseException
Throws:
ParseException

mergeArguments

protected void mergeArguments()
Adds command line arguments to options file arguments, as a way of overriding and supplementing arguments.


shouldPrintHelp

protected boolean shouldPrintHelp()
Protected method that determines if help should be printed based on a set of known-criteria such as error conditions.

Returns:
True if help should be printed, false otherwise.

parseSpecific

protected void parseSpecific()
                      throws ParseException
Wrapper method that dispatches specific parse checking based on action id. If the printHelp member is set, then no specific parsing is performed.

Throws:
ParseException

parseAccept

protected void parseAccept()
                    throws ParseException
Parsing for fei5accept fei5accept <filetype> for <add|replace|get|delete> {[output <path>] [crc] [restart] [saferead] [autodelete] [replace|version]}

Throws:
ParseException

parseAdd

protected void parseAdd()
                 throws ParseException
Parsing for fei5add fei5add [ <server group>:] <file type> <file name expression>... {[before|after <date-time>] | [between <date-time1>and <date-time2>] [comment " <comment text>"] [crc] [autodelete] [help]} fei5add using <option file> Option File Format (per line): [ <server group>:] <file type> <file name>... {[before|after <date-time>] | [between <date-time1>and <date-time2>] [comment " <comment text>"] [autodelete] [crc]}

Throws:
ParseException

parseCheck

protected void parseCheck()
                   throws ParseException
Parsing for fei5check fei5check

Throws:
ParseException

parseCheckfiles

protected void parseCheckfiles()
                        throws ParseException
Parsing for fei5checkfiles fei5checkfiles [ <server group>:] <file type> ["<file name expression>"] {[before|after <date-time>] | [between <date-time1>and <date-time2>] [long | verylong] [help]} fei5checkfiles using <option file> Option File Format (per line): [ <server group>:] <file type> " <file name expression>" {[before|after <date-time>] | [between <date-time1>and <date-time2>] [long | verylong]}

Throws:
ParseException

parseComment

protected void parseComment()
                     throws ParseException
Parsing for fei5comment fei5comment [ <server group>:] <file type> <file name> comment " <comment text>" [help]

Throws:
ParseException

parseCrc

protected void parseCrc()
                 throws ParseException
Parsing for fei5crc fei5crc <file name expression>

Throws:
ParseException

parseDelete

protected void parseDelete()
                    throws ParseException
Parsing for fei5delete fei5delete [ <server group>:] <file type> " <file name expression>" [help] fei5delete using <option file> Option File Format (per line): [ <server group>:] <file type> <file name>

Throws:
ParseException

parseMakeClean

protected void parseMakeClean()
                       throws ParseException
Throws:
ParseException

parseDisplay

protected void parseDisplay()
                     throws ParseException
Parsing for fei5display fei5display [ <server group>:] <file type> <file name> [help]

Throws:
ParseException

parseGet

protected void parseGet()
                 throws ParseException
Parsing for fei5get fei5get [ <server group>:] <file type> [" <file name expression>"] {[output <path>] [before|after <datetime>] | [between <datetime1>and <datetime2>] [crc] [saferead] [receipt] [replace|version] [help]} fei5get using <option file> Option File Format (per line): [ <server group>:] <file type> [" <file name expression>"] {[output <path>] [before|after <date-time>] | [between <date-time1>and <date-time2>] [crc] [saferead] [receipt] [replace|version]}

Throws:
ParseException

parseKdestroy

protected void parseKdestroy()
                      throws ParseException
Parsing for fei5kdestroy fei5kdestroy

Throws:
ParseException

parseKinit

protected void parseKinit()
                   throws ParseException
Parsing for fei5kinit fei5kinit

Throws:
ParseException

parseKlist

protected void parseKlist()
                   throws ParseException
Parsing for fei5klist fei5klist

Throws:
ParseException

parseList

protected void parseList()
                  throws ParseException
Parsing for fei5list fei5list [ <server group>:] <file type> [" <file name expression>"] {[before|after <date-time>] | [between <date-time1>and <date-time2>] [long | verylong] [help]}

Throws:
ParseException

parseMakeclean

protected void parseMakeclean()
                       throws ParseException
Parsing for fei5makeclean fei5makeclean [ <server group>:] <file type> "<file name expression>" [help]

Throws:
ParseException

parseReference

protected void parseReference()
                       throws ParseException
Parsing for fei5reference fei5reference [ <server group>:] <file type> <file name> vft <VFT name> reference <ref name> [help]

Throws:
ParseException

parseRegister

protected void parseRegister()
                      throws ParseException
Parsing for fei5register: fei5register [ <server group>:] <file type> <file name expression>... {[before|after <date-time>] | [between <date-time1>and <date-time2>] [comment " <comment text>"] [help]} fei5register using <option file> Option File Format (per line): [ <server group>:] <file type> <file name>... {[before|after <date-time>] | [between <date-time1>and <date-time2>] [comment " <comment text>"]}

Throws:
ParseException

parseUnregister

protected void parseUnregister()
                        throws ParseException
Parsing for fei5unregister: fei5unregister [ <server group>:] <file type> " <file name expression>" [help] fei5unregister using <option file> Option File Format (per line): [ <server group>:] <file type> <file name>

Throws:
ParseException

parseRename

protected void parseRename()
                    throws ParseException
Parsing for fei5rename fei5rename [ <server group>:] <file type> <old file name> <new file name> [help] fei5rename using <option file> Option File Format (per line): [ <server group>:] <file type> <old file name> <new file name>

Throws:
ParseException

parseReplace

protected void parseReplace()
                     throws ParseException
Parsing for fei5replace fei5replace [ <server group>:] <file type> <file name expression>... {[before|after <date-time>] | [between <date-time1> and <date-time2>] [comment " <comment text>"] [crc] [autodelete] [help]} fei5replace using <option file> Option File Format (per line): [ <server group>:] <file type> <file name>... {[before|after <date-time>] | [between <date-time1>and <date-time2>] [comment " <comment text>"] [autodelete] [crc]}

Throws:
ParseException

parseShowtypes

protected void parseShowtypes()
                       throws ParseException
Parsing for fei5showtypes

Throws:
ParseException

parseNotify

protected void parseNotify()
                    throws ParseException
Parsing for fei5notify fei5notify [ <server group:] <file type> {[output <path>] [restart] [using <option file>]} Option File Format: invoke <command> invokeExitOnError invokeAsync logFile <file name> mailMessageFrom <email address> mailMessageTo <email address, email address, ...> mailReportAt <hh:mm am|pm, hh:mm am|pm, ...> mailReportTo <email address, email address, ...> mailSMTPHost <host name>

Throws:
ParseException

parseSubscribe

protected void parseSubscribe()
                       throws ParseException
Parsing for fei5subscribe fei5subscribe [ <server group:] <file type> {[output <path>] [restart] [using <option file>]} Option File Format: crc invoke <command>invokeExitOnError logFile <file name> mailMessageFrom <email address> mailMessageTo <email address, email address, ...> mailReportAt <hh:mm am|pm, hh:mm am|pm, ...> mailReportTo <email address, email address, ...> mailSMTPHost <host name> receipt replace saferead version

Throws:
ParseException

parseArgs

protected boolean parseArgs(String[] args,
                            Hashtable argTable)
                     throws ParseException
Parse the command line arguments.

Parameters:
args - The command line arguments
argTable - the argument lookup hashtable
Returns:
boolean true if parsing was successful, false otherwise
Throws:
ParseException - when parsing fails

parseOptionsFile

protected void parseOptionsFile(String optionsFilename)
                         throws ParseException
Parses entries from an options file and populates the fileArguments list. If actionId is subscription or notify, then contents of file are for a single operation invocation, and thus the list will contain a single entry. For all other operations, each line represents a separate invocation, and so the list size should be equal to the number of non-empty lines of an options file.

Parameters:
optionsFilename - Path of the options file to be parsed
Throws:
ParseException - if a parse-related error occurs

buildArgs

protected String[] buildArgs(String line)
                      throws ParseException
Method to parse a line read in from the option file to create a tokenized array of strings.

Parameters:
line - the input line string
Returns:
the tokenized array of strings
Throws:
ParseException - when invalid syntax is encountered

getDate

public static Date getDate(String time,
                           boolean pm)
Translates from string representation of time to Date object.

Parameters:
time - Time as string, format hh:mm
pm - flag indicating time is in PM
Returns:
Date object represented by parameters

parseTimeList

public static Date[] parseTimeList(String timelist)
Returns an array of Date objects created from the timelist parameter.

Parameters:
timelist - String of date list delimited by comma (,), where each entry is of the form 'hh:mm [ap]m'
Returns:
Array of Date corresponding to the timelist

toString

public String toString()
Override of toString method. Prints the super.toString() followed by the contents of the parse mapping.

Overrides:
toString in class Object
Returns:
String representation of this instance

setError

protected int setError(int lineNumber)
Helper method that sets the error flag and returns parameter value back.

Parameters:
lineNumber - Line number associated with error
Returns:
lineNumber value