Download | Plain Text | No Line Numbers


  1. /*
  2.  * Copyright (c) 2010, Manuel Mausz. All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * - Redistributions of source code must retain the above copyright
  9.  * notice, this list of conditions and the following disclaimer.
  10.  *
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  * notice, this list of conditions and the following disclaimer in the
  13.  * documentation and/or other materials provided with the distribution.
  14.  *
  15.  * - The names of the authors may not be used to endorse or promote products
  16.  * derived from this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  20.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  */
  30.  
  31. import java.io.InputStreamReader;
  32. import java.io.BufferedReader;
  33. import java.io.InputStream;
  34. import java.io.IOException;
  35.  
  36. /*
  37.  * Allows you to easily setup a interactive console application
  38.  * Register commands in ctor and invoke run() afterwards
  39.  * Reads line-based
  40.  *
  41.  * @author Manuel Mausz (0728348)
  42.  */
  43. public abstract class CommandInteractive
  44. {
  45. private boolean stop = false;
  46. private boolean onecommand = false;
  47. private boolean ignoreempty = true;
  48. protected CommandHandler cmdHandler;
  49.  
  50. /*--------------------------------------------------------------------------*/
  51.  
  52. public CommandInteractive()
  53. {
  54. cmdHandler = new CommandHandler();
  55. }
  56.  
  57. /*--------------------------------------------------------------------------*/
  58.  
  59. public void setOneCommandMode(boolean mode)
  60. {
  61. onecommand = mode;
  62. }
  63.  
  64. /*--------------------------------------------------------------------------*/
  65.  
  66. public void setIgnoreEmptyMode(boolean mode)
  67. {
  68. ignoreempty = mode;
  69. }
  70.  
  71. /*--------------------------------------------------------------------------*/
  72.  
  73. public void stop()
  74. {
  75. stop = true;
  76. }
  77.  
  78. /*--------------------------------------------------------------------------*/
  79.  
  80. public void printPrompt()
  81. {}
  82.  
  83. /*--------------------------------------------------------------------------*/
  84.  
  85. public void run(InputStream in)
  86. throws IOException
  87. {
  88. BufferedReader stream = new BufferedReader(new InputStreamReader(in));
  89. String input;
  90.  
  91. printPrompt();
  92. while(!stop && (input = stream.readLine()) != null)
  93. {
  94. if (!ignoreempty || input.length() > 0)
  95. {
  96. String[] pieces = input.split("\\s+");
  97. String[] args = new String[pieces.length - 1];
  98. System.arraycopy(pieces, 1, args, 0, pieces.length - 1);
  99. cmdHandler.call(pieces[0], args);
  100. }
  101.  
  102. if (onecommand)
  103. stop();
  104.  
  105. if (!stop)
  106. printPrompt();
  107. }
  108. }
  109. }
  110.