Class AbstractConsole

  • All Implemented Interfaces:
    Runnable
    Direct Known Subclasses:
    Console

    public abstract class AbstractConsole
    extends Object
    implements Runnable
    Base class for application performing operations on WKT objects from the command line. Instructions are usually read from the standard input stream and results sent to the standard output stream, but those streams can be redirected. The set of allowed instructions depends on the subclass used.
    Since:
    2.1
    Author:
    Martin Desruisseaux (IRD)
    • Constructor Detail

      • AbstractConsole

        public AbstractConsole​(Format parser,
                               LineNumberReader in)
        Creates a new console instance using the specified input stream.
        Parameters:
        parser - The WKT parser, usually a Preprocessor object.
        in - The input stream.
      • AbstractConsole

        public AbstractConsole​(Format parser,
                               LineNumberReader in,
                               Writer out,
                               PrintWriter err,
                               String lineSeparator)
        Creates a new console instance using the specified streams and line separator.
        Parameters:
        parser - The WKT parser, usually a Preprocessor object.
        in - The input stream.
        out - The output stream.
        err - The error stream.
        lineSeparator - The line separator.
    • Method Detail

      • addDefinition

        public void addDefinition​(String name,
                                  String value)
                           throws ParseException
        Adds a predefined Well Know Text (WKT). The value argument given to this method can contains itself other definitions specified in some previous calls to this method. This method do nothing if the parser is not an instance of Preprocessor.
        Parameters:
        name - The name for the definition to be added.
        value - The Well Know Text (WKT) represented by the name.
        Throws:
        IllegalArgumentException - if the name is invalid.
        ParseException - if the WKT can't be parsed.
      • loadDefinitions

        public void loadDefinitions​(LineNumberReader in)
                             throws IOException,
                                    ParseException
        Load all definitions from the specified stream. Definitions are key-value pairs in the form name = wkt (without the SET keyword). The result is the same than invoking the SET instruction for each line in the specified stream. This method is used for loading predefined objects like the database used by PropertyAuthorityFactory.
        Parameters:
        in - The input stream.
        Throws:
        IOException - if an input operation failed.
        ParseException - if a well know text (WKT) can't be parsed.
      • getPrompt

        public String getPrompt()
        Returns the command-line prompt, or null if there is none.
      • setPrompt

        public void setPrompt​(String prompt)
        Set the command-line prompt, or null for none.
      • run

        public void run()
        Process instructions from the input stream specified at construction time. All lines are read until the end of stream ([Ctrl-Z] for input from the keyboard), or until stop() is invoked. Non-empty and non-comment lines are given to the execute(java.lang.String) method. Errors are catched and printed to the error stream.
        Specified by:
        run in interface Runnable
      • executeAll

        public void executeAll()
                        throws Exception
        Executes all instructions (like run()), but stop at the first error.
        Throws:
        Exception - if an instruction failed.
      • execute

        protected abstract void execute​(String instruction)
                                 throws Exception
        Execute the specified instruction.
        Parameters:
        instruction - The instruction to execute.
        Throws:
        Exception - if the instruction failed.
      • 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.
      • reportError

        protected void reportError​(Exception exception)
        Print an exception message to the standard error stream. The error message includes the line number, and the column where the failure occured in the exception is an instance of ParseException.
        Parameters:
        exception - The exception to report.