org.geotoolkit.console
Class InteractiveConsole

Object
  extended by InteractiveConsole
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
ReferencingConsole

public abstract class InteractiveConsole
extends Object
implements Runnable

Base class for applications executing instructions from the console. If there is no console attached to the current JVM, then this class will read instructions from standard input stream and sent the results to the standard output stream. But different streams can be explicitly specified at construction time.

Every empty lines, and every lines beginning with "//" or "#" characters, are ignored. Other lines are sent to the execute method, which subclasses should override.

Since:
2.1
Version:
3.00
Author:
Martin Desruisseaux (IRD)
Module:
utility/geotk-utility (download)    View source code for this class

Field Summary
protected  PrintWriter err
          The error stream.
protected  PrintWriter out
          The output stream.
 
Constructor Summary
protected InteractiveConsole(CommandLine commands)
          Creates a new instance using the console if available, or the parameters from the command line otherwise.
protected InteractiveConsole(LineNumberReader in)
          Creates a new instance using the specified input stream.
protected InteractiveConsole(LineNumberReader in, PrintWriter out)
          Creates a new instance using the specified input and output streams.
 
Method Summary
protected  void execute(String instruction)
          Executes the given instruction.
 String getPrompt()
          Returns the command-line prompt, or null if there is no console.
protected  void reportError(Exception exception)
          Invoked by run() if an exception occurred in the execute method.
 void run()
          Executes all instructions from the console or the input stream.
 void setPrompt(String prompt)
          Sets the command-line prompt, which may be an empty string.
protected  void setSymbols(char[] openingBrackets, char[] closingBrackets, char quote)
          Gives some hints about the expected syntax of commands.
 void stop()
          Stops the run() method.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

out

protected final PrintWriter out
The output stream.


err

protected final PrintWriter err
The error stream.

Constructor Detail

InteractiveConsole

protected InteractiveConsole(CommandLine commands)
Creates a new instance using the console if available, or the parameters from the command line otherwise. If no command line is given, then this constructor fallback on the standard input stream, standard output stream and error output stream

Parameters:
commands - The command line, or null if none.
Since:
3.00

InteractiveConsole

protected InteractiveConsole(LineNumberReader in)
Creates a new instance using the specified input stream. The output and error stream still the system ones.

Parameters:
in - The input stream (can not be null).
Since:
3.00

InteractiveConsole

protected InteractiveConsole(LineNumberReader in,
                             PrintWriter out)
Creates a new instance using the specified input and output streams. The error stream still the system one.

Parameters:
in - The input stream (can not be null).
out - The output stream (can not be null).
Since:
3.00
Method Detail

setSymbols

protected void setSymbols(char[] openingBrackets,
                          char[] closingBrackets,
                          char quote)
Gives some hints about the expected syntax of commands. By default this class performs no analysis on the input strings. If this method is given non-empty lists of bracket characters, then the behavior of this class regarding input lines is modified as below:

Parameters:
openingBrackets - List of caracters acceptable as opening bracket. The closing bracket must be the character in the closingBrackets array at the same index than the opening bracket.
closingBrackets - List of caracters acceptable as closing bracket.
quote - The character used for quoting texts, or 0 if none.
Since:
3.00

setPrompt

public void setPrompt(String prompt)
Sets the command-line prompt, which may be an empty string. This method does nothing if this class is not using the console attached to the JVM.

Parameters:
prompt - The new command-line prompt.

getPrompt

public String getPrompt()
Returns the command-line prompt, or null if there is no console. This method returns a non-null value only if this class is using the console attached to the JVM.

Returns:
The command-line prompt, or null if there is no attached console.

stop

public void stop()
Stops the run() method. This method can been invoked from any thread. If a line is in process, it will be finished before the run() method stops.


run

public void run()
Executes all instructions from the console or the input stream. Lines are read until the end of stream is reached ([Ctrl-Z] for input from the keyboard), or until the stop() method is invoked. Empty and comment lines are ignored.

Multilines may be concatenated in a single instruction if a bracket is open on a line and closed only a few lines later. Once an instruction is assembled, it is given to the execute method. If an exception occurs during the call to execute, then the reportError method is invoked. The later can stop the execution either by invoking the stop method or by throwing an exception.

Specified by:
run in interface Runnable

execute

protected void execute(String instruction)
                throws Exception
Executes the given instruction. This method is invoked by run() for each line (or group of lines) read fron the console or the input stream.

The default implementation recognizes the stacktrace and exit, and prints an error message for all other instructions. Subclasses should override this method is order to provide more functionalities.

Parameters:
instruction - The instruction to execute.
Throws:
Exception - if the instruction failed.

reportError

protected void reportError(Exception exception)
Invoked by run() if an exception occurred in the execute method. The default implementation prints a message to the error stream. The error message includes the line number if this information is available.

Parameters:
exception - The exception to report.
TODO:
Localize


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.