1 /*
2 * Zemucan: A Syntax Assistant for DB2
3 * Copyright (C) 2009, 2010 Andres Gomez Casanova
4 *
5 * This file is part of Zemucan.
6 *
7 * Zemucan is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * Zemucan is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, see <http://www.gnu.org/licenses/>.
19 *
20 * Contact:
21 * a n g o c a at y a h o o dot c o m
22 * Cra. 45 No 61 - 31, Bogota, Colombia.
23 *
24 * Author: $LastChangedBy: angoca $:
25 * Date: $LastChangedDate: 2011-03-06 23:10:20 -0500 (dom, 06 mar 2011) $:
26 * Revision: $LastChangedRevision: 1917 $:
27 * URL: $HeadURL: https://zemucan.svn.sourceforge.net/svnroot/zemucan/branches/zemucan_v1/source-code/uiImplSystem/src/main/java/name/angoca/zemucan/ui/impl/system/SystemInputReader.java $:
28 */
29 package name.angoca.zemucan.ui.impl.system;
30
31 import java.io.IOException;
32
33 import name.angoca.zemucan.ui.api.AbstractUIException;
34 import name.angoca.zemucan.ui.api.InputReader;
35 import name.angoca.zemucan.ui.api.InputReaderException;
36 import name.angoca.zemucan.ui.api.OutputWriter;
37
38 /**
39 * Implementation of the input reader using the System.In option. This class
40 * flushes the stream before the read is started.
41 * <p>
42 * <b>Control Version</b>
43 * <p>
44 * <ul>
45 * <li>0.0.1 Class creation.</li>
46 * <li>0.0.2 No override.</li>
47 * <li>0.0.3 finals.</li>
48 * <li>1.0.0 Moved to version 1.</li>
49 * <li>1.1.0 Exception hierarchy changed.</li>
50 * <li>1.1.1 try catch.</li>
51 * <li>1.1.2 Asserts.</li>
52 * </ul>
53 *
54 * @author Andres Gomez Casanova <a
55 * href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
56 * @version 1.1.2 2009-09-27
57 */
58 public final class SystemInputReader implements InputReader {
59
60 /**
61 * OutputWriter to write the prompt.
62 */
63 private final transient OutputWriter output;
64 /**
65 * Prompt to show in the console.
66 */
67 private final transient String prompt;
68
69 /**
70 * Constructor that flushes the stream.
71 *
72 * @param outputWriter
73 * OutputWriter to write the prompt.
74 * @param consolePrompt
75 * Prompt to show in each line of the console.
76 * @throws InputReaderException
77 * If there is a problem flushing the stream.
78 */
79 public SystemInputReader(final OutputWriter/* ! */outputWriter,
80 final String /* ! */consolePrompt) throws InputReaderException {
81 assert outputWriter != null;
82 assert consolePrompt != null;
83
84 try {
85 this.flush();
86 } catch (final IOException exception) {
87 throw new InputReaderException(exception);
88 }
89 this.output = outputWriter;
90 this.prompt = consolePrompt;
91 }
92
93 /**
94 * Flushes the stream. It is a good idea to call it before the read is
95 * started.
96 *
97 * @throws IOException
98 * If there is a problem when reading.
99 */
100 private void flush() throws IOException {
101 while ((System.in.available()) != 0) {
102 System.in.read();
103 }
104 }
105
106 /*
107 * (non-Javadoc)
108 *
109 * @see name.angoca.zemucan.ui.api.InputReader#readString()
110 */
111 @Override
112 public String/* ! */readString() throws AbstractUIException {
113 this.output.writeString(this.prompt);
114
115 int character;
116 final StringBuffer string = new StringBuffer();
117 boolean finished = false;
118 while (!finished) {
119 try {
120 character = System.in.read();
121 } catch (final IOException exception) {
122 throw new InputReaderException(exception);
123 }
124
125 if ((character < 0) || ((char) character == '\n')) {
126 finished = true;
127 } else if ((char) character != '\r') {
128 string.append((char) character);
129 }
130 }
131 final String ret = string.toString();
132
133 assert ret != null;
134 return ret;
135 }
136 }