1 /**
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Intalio, Inc. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Intalio, Inc. Exolab is a registered
23 * trademark of Intalio, Inc.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2000-2001 (C) Intalio, Inc. All Rights Reserved.
42 *
43 * $Id$
44 */
45 package org.exolab.castor.util.dialog;
46
47 /**
48 * A simple utility class to handle command line dialogs
49 *
50 * @author <a href="mailto:nsgreen@thazar.com">Nathan Green</a>
51 * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
52 */
53 public class ConsoleDialog implements Dialog {
54
55 /**
56 * Creates a new Console Dialog
57 */
58 public ConsoleDialog () {
59 super();
60 } //-- ConsoleDialog
61
62 /**
63 * Presents a confirmation prompt with the given message.
64 *
65 * @param message
66 * the confirmation prompt message to display
67 * @return true if the user has selected a positive confirmation, otherwise
68 * false
69 */
70 public boolean confirm(final String message) {
71 try {
72 while (true) {
73 System.out.println();
74 System.out.print(message);
75 System.out.print("(y|n|?) : ");
76
77 int ch = getChar();
78
79 System.out.println();
80
81 //-- check ch
82 switch (ch) {
83 case 'y':
84 return true;
85 case 'n':
86 return false;
87 case '?':
88 System.out.println("y = yes, n = no");
89 break;
90 default:
91 System.out.print("invalid input, expecting ");
92 System.out.println("'y', 'n', or '?'.");
93 break;
94 }
95 }
96 } catch (java.io.IOException ix) {
97 System.out.println(ix);
98 }
99 return false;
100 } //-- confirm
101
102 /**
103 * Returns a single char from System.in.
104 *
105 * @return the character entered, or null if more than one was entered (not
106 * including EOLs)
107 * @throws java.io.IOException
108 * if one is encountered in System.in.read()
109 */
110 private int getChar() throws java.io.IOException {
111 int ch = System.in.read();
112
113 //-- read eoln, or extra characters
114 while (System.in.available() > 0) {
115 switch (System.in.read()) {
116 case '\n':
117 case '\r':
118 break;
119 default:
120 ch = '\0';
121 }
122 }
123 return ch;
124 }
125
126 /**
127 * Presents a confirmation prompt for values with the given messge.
128 *
129 * @param message
130 * the confirmation prompt to display
131 * @param values
132 * a list of valid characters to accept
133 * @return whatever character the user presses
134 */
135 public char confirm(final String message, final String values) {
136 return confirm(message, values, "no help available...");
137 }
138
139 /**
140 * Presents a confirmation prompt for values with the given messge
141 *
142 * @param message
143 * the confirmation prompt to display
144 * @param values
145 * a list of valid characters to accept
146 * @param help
147 * a help message when the user presses '?'
148 * @return whatever character the user presses
149 */
150 public char confirm(final String message, final String values, final String help) {
151 String prompt = makeList(values);
152
153 try {
154 while (true) {
155 System.out.println();
156 System.out.print(message + prompt);
157
158 int ch = getChar();
159
160 System.out.println();
161
162 //-- check ch
163 if (values.indexOf(ch) != -1) {
164 return (char) ch;
165 }
166
167 if (ch == '?') {
168 System.out.println(help);
169 } else {
170 System.out.print("invalid input, expecting ");
171 System.out.println(listInput(values));
172 }
173 }
174 } catch (java.io.IOException ix) {
175 System.out.println(ix);
176 }
177 return '\0';
178 }
179
180 /**
181 * Displays the given message to the user. No input is returned from
182 * the user.
183 *
184 * @param message the message to display to the user
185 */
186 public void notify(final String message) {
187 System.out.println(message);
188 } //-- notify
189
190 /**
191 * Converts a list of characters into a delimited prompt. A '?' is
192 * automatically put at the end.
193 *
194 * @param values
195 * a list of valid characters to accept
196 * @return each character separated by a pipe and in parenthesis
197 */
198 private String makeList(final String values) {
199 StringBuffer sb = new StringBuffer(values.length() * 2);
200 sb.append('(');
201 for (int i = 0; i < values.length(); i++) {
202 sb.append(values.charAt(i)).append('|');
203 }
204 sb.append("?)");
205 return sb.toString();
206 }
207
208 /**
209 * Creates a list of valid input options to give a better explanation to the
210 * user.
211 *
212 * @param values
213 * a list of valid characters to accept
214 * @return each character in single quotes, comma separated
215 */
216 private String listInput(final String values) {
217 StringBuffer sb = new StringBuffer(values.length() * 4);
218 for (int i = 0; i < values.length(); i++) {
219 sb.append('\'') .append(values.charAt(i)) .append("', ");
220 }
221 sb.append("or '?'");
222 return sb.toString();
223 }
224
225 } //-- Dialog