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