lapis
Class Arguments

java.lang.Object
  extended bylapis.Arguments
All Implemented Interfaces:
Enumeration
Direct Known Subclasses:
TclArguments, ToolAction.BrowserArguments

public class Arguments
extends Object
implements Enumeration


Nested Class Summary
protected static class Arguments.Name
          Parameter name.
static class Arguments.UsageException
          Exception thrown when argument processing encounters an error.
 
Field Summary
protected  Vector argv
          Argument list.
protected  int currArg
          Index of current argument (returned by getElement()).
static Debug debug
           
protected  Document input
           
protected  Interpreter interpreter
           
protected  Object[] originalArgs
           
protected  DocumentWriter output
           
protected  String usageMessage
           
 
Constructor Summary
Arguments(Interpreter interpreter)
          Make an empty Arguments object.
 
Method Summary
 Arguments add(String name, Object value)
          Add a name/value pair to the argument sequence.
 Arguments addArguments(Arguments args)
          Concatenates another argument sequence to the argument sequence, consuming the other sequence entirely.
 Arguments addName(String name)
          Add a parameter name to the argument sequence.
 Arguments addValue(Object arg)
          Add a parameter value to the argument sequence.
 void consume(String name)
          Handle a parameter common to all LAPIS tools.
protected  String getContextMessage()
          Get a representation of the command line with the current argument position marked in it, to be printed with the usage message.
 Object getElement()
          Get current argument (either name or value).
 DocumentFactory getFactory()
           
 Document getInput()
          Get input document specified by arguments.
 Interpreter getInterpreter()
           
 Object[] getOriginalArguments()
           
 DocumentWriter getOutput()
          Get output stream specified by arguments.
 String getUsage()
          Get usage message printed when an erroneous argument is found.
 boolean hasMoreElements()
          Test whether argument enumeration is exhausted.
 boolean isName()
          Test whether current argument is a name or a value.
 Object nextElement()
          Get argument (either name or value) and advance to next argument.
 int nextInt()
          Get next argument as an integer.
 String nextName()
          Try to get argument as a parameter name and advance to next argument if successful.
 TC nextPattern()
          Get next argument as a TC pattern.
 String nextString()
          Get next argument as a String.
 Object nextValue()
          Try to get argument as a parameter value and advance to next argument if successful.
 Arguments setInput(Document doc)
          Set input document.
 Arguments setInput(Object obj)
          Set input document.
 Arguments setOutput(DocumentWriter out)
          Set output document.
 void setUsage(String message)
          Set usage message printed when an erroneous argument is found.
 void usage()
          Fire a UsageException containing the usage message set by setUsage().
 void usage(String message)
          Fire a UsageException containing the given message.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

debug

public static Debug debug

argv

protected Vector argv
Argument list.


currArg

protected int currArg
Index of current argument (returned by getElement()).


originalArgs

protected Object[] originalArgs

interpreter

protected Interpreter interpreter

input

protected Document input

output

protected DocumentWriter output

usageMessage

protected String usageMessage
Constructor Detail

Arguments

public Arguments(Interpreter interpreter)
Make an empty Arguments object.

Method Detail

add

public Arguments add(String name,
                     Object value)
Add a name/value pair to the argument sequence.

Parameters:
name - Parameter name (may be null)
value - Parameter value (may be null)
Returns:
this Arguments object (useful for stacking up add() calls, e.g. new Arguments().add ("X", "5").add("Y", "6");

addName

public Arguments addName(String name)
Add a parameter name to the argument sequence.

Returns:
this Arguments object (useful for stacking up add() calls).

addValue

public Arguments addValue(Object arg)
Add a parameter value to the argument sequence.

Parameters:
arg - Parameter value
Returns:
this Arguments object (useful for stacking up add() calls).

addArguments

public Arguments addArguments(Arguments args)
Concatenates another argument sequence to the argument sequence, consuming the other sequence entirely.

Parameters:
args - Arguments to add
Returns:
this Arguments object (useful for stacking up add() calls).

hasMoreElements

public boolean hasMoreElements()
Test whether argument enumeration is exhausted.

Specified by:
hasMoreElements in interface Enumeration
Returns:
true if more arguments to read (so getElement() or nextElement() would succeed), false if not

getElement

public Object getElement()
Get current argument (either name or value).

Returns:
argument
Throws:
Arguments.UsageException - if no more arguments

isName

public boolean isName()
Test whether current argument is a name or a value.

Returns:
true if name, false if not
Throws:
Arguments.UsageException - if no more arguments

nextElement

public Object nextElement()
Get argument (either name or value) and advance to next argument.

Specified by:
nextElement in interface Enumeration
Returns:
argument
Throws:
Arguments.UsageException - if no more arguments

nextName

public String nextName()
Try to get argument as a parameter name and advance to next argument if successful.

Returns:
parameter name, or null if current argument is not a name.

nextValue

public Object nextValue()
Try to get argument as a parameter value and advance to next argument if successful.

Returns:
parameter value, or null if current argument is not a value.

nextString

public String nextString()
                  throws Arguments.UsageException
Get next argument as a String.

Returns:
next argument
Throws:
Arguments.UsageException - if next argument is a parameter name, not a value

nextInt

public int nextInt()
            throws Arguments.UsageException,
                   NumberFormatException
Get next argument as an integer.

Returns:
integer value of next argument
Throws:
Arguments.UsageException - if next argument is a parameter name, not a value
NumberFormatException - if argument isn't an integer

nextPattern

public TC nextPattern()
               throws Arguments.UsageException,
                      TCParseException
Get next argument as a TC pattern. Argument may be a String, a TC, or a NamedRegionSet.

Returns:
TC created from next argument
Throws:
Arguments.UsageException - if next argument is a parameter name, not a value, or if it's the wrong type
TCParseException - if argument isn't understood be loaded

consume

public void consume(String name)
             throws Exception
Handle a parameter common to all LAPIS tools.

Parameters:
name - Parameter name returned by nextName(). No other nextX() call should have been made since the call to nextName().
Throws:
Arguments.UsageException - if not a common parameter
Exception - if some other exception occurs in processing the parameter.

getOriginalArguments

public Object[] getOriginalArguments()

getInput

public Document getInput()
Get input document specified by arguments. If no input document was specified in the arguments, then input comes from the current document.

Call after processing all arguments (and calling consume() on arguments that the tool doesn't want to handle).

Returns:
input document

setInput

public Arguments setInput(Document doc)
Set input document.

Parameters:
doc - Input document
Returns:
this Arguments object (useful for stacking up set() and add() calls)

setInput

public Arguments setInput(Object obj)
                   throws IOException
Set input document.

Parameters:
obj - Object to convert to an input document
Returns:
this Arguments object (useful for stacking up set() and add() calls)
Throws:
IOException

getOutput

public DocumentWriter getOutput()
Get output stream specified by arguments. Call only after processing all arguments and calling consume() on arguments that the tool doesn't want to handle.

Returns:
DocumentWriter to which tool should send its output

setOutput

public Arguments setOutput(DocumentWriter out)
Set output document.

Returns:
this Arguments object (useful for stacking up set() and add() calls)

getInterpreter

public Interpreter getInterpreter()

getFactory

public DocumentFactory getFactory()

getUsage

public String getUsage()
Get usage message printed when an erroneous argument is found. Usage message is null by default; must be set by tool before argument processing.

Returns:
tool usage message

setUsage

public void setUsage(String message)
Set usage message printed when an erroneous argument is found. Call before argument processing.

Returns:
tool usage message

usage

public void usage()
Fire a UsageException containing the usage message set by setUsage(). Never returns.

Throws:
Arguments.UsageException - thrown always

usage

public void usage(String message)
Fire a UsageException containing the given message. Never returns.

Parameters:
message - Usage message to include in thrown UsageException
Throws:
Arguments.UsageException - thrown always

getContextMessage

protected String getContextMessage()
Get a representation of the command line with the current argument position marked in it, to be printed with the usage message.

Returns:
command line (a string ending with a newline)