1 18 19 package org.apache.tools.ant.input; 20 21 import java.io.BufferedReader ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.util.Enumeration ; 26 import org.apache.tools.ant.BuildException; 27 28 33 public class DefaultInputHandler implements InputHandler { 34 35 38 public DefaultInputHandler() { 39 } 40 41 47 public void handleInput(InputRequest request) throws BuildException { 48 String prompt = getPrompt(request); 49 BufferedReader r = null; 50 try { 51 r = new BufferedReader (new InputStreamReader (getInputStream())); 52 do { 53 System.err.println(prompt); 54 System.err.flush(); 55 try { 56 String input = r.readLine(); 57 request.setInput(input); 58 } catch (IOException e) { 59 throw new BuildException("Failed to read input from" 60 + " Console.", e); 61 } 62 } while (!request.isInputValid()); 63 } finally { 64 if (r != null) { 65 try { 66 r.close(); 67 } catch (IOException e) { 68 throw new BuildException("Failed to close input.", e); 69 } 70 } 71 } 72 } 73 74 84 protected String getPrompt(InputRequest request) { 85 String prompt = request.getPrompt(); 86 String def = request.getDefaultValue(); 87 if (request instanceof MultipleChoiceInputRequest) { 88 StringBuffer sb = new StringBuffer (prompt); 89 sb.append(" ("); 90 Enumeration e = 91 ((MultipleChoiceInputRequest) request).getChoices().elements(); 92 boolean first = true; 93 while (e.hasMoreElements()) { 94 if (!first) { 95 sb.append(", "); 96 } 97 String next = (String ) e.nextElement(); 98 if (next.equals(def)) { 99 sb.append('['); 100 } 101 sb.append(next); 102 if (next.equals(def)) { 103 sb.append(']'); 104 } 105 first = false; 106 } 107 sb.append(")"); 108 return sb.toString(); 109 } else if (def != null) { 110 return prompt + " [" + def + "]"; 111 } else { 112 return prompt; 113 } 114 } 115 116 120 protected InputStream getInputStream() { 121 return System.in; 122 } 123 124 } 125 | Popular Tags |