KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > input > DefaultInputHandler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.input;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import org.apache.tools.ant.BuildException;
27
28 /**
29  * Prompts on System.err, reads input from System.in
30  *
31  * @since Ant 1.5
32  */

33 public class DefaultInputHandler implements InputHandler {
34
35     /**
36      * Empty no-arg constructor
37      */

38     public DefaultInputHandler() {
39     }
40
41     /**
42      * Prompts and requests input. May loop until a valid input has
43      * been entered.
44      * @param request the request to handle
45      * @throws BuildException if not possible to read from console
46      */

47     public void handleInput(InputRequest request) throws BuildException {
48         String JavaDoc prompt = getPrompt(request);
49         BufferedReader JavaDoc r = null;
50         try {
51             r = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(getInputStream()));
52             do {
53                 System.err.println(prompt);
54                 System.err.flush();
55                 try {
56                     String JavaDoc input = r.readLine();
57                     request.setInput(input);
58                 } catch (IOException JavaDoc 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 JavaDoc e) {
68                     throw new BuildException("Failed to close input.", e);
69                 }
70             }
71         }
72     }
73
74     /**
75      * Constructs user prompt from a request.
76      *
77      * <p>This implementation adds (choice1,choice2,choice3,...) to the
78      * prompt for <code>MultipleChoiceInputRequest</code>s.</p>
79      *
80      * @param request the request to construct the prompt for.
81      * Must not be <code>null</code>.
82      * @return the prompt to ask the user
83      */

84     protected String JavaDoc getPrompt(InputRequest request) {
85         String JavaDoc prompt = request.getPrompt();
86         String JavaDoc def = request.getDefaultValue();
87         if (request instanceof MultipleChoiceInputRequest) {
88             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(prompt);
89             sb.append(" (");
90             Enumeration JavaDoc e =
91                 ((MultipleChoiceInputRequest) request).getChoices().elements();
92             boolean first = true;
93             while (e.hasMoreElements()) {
94                 if (!first) {
95                     sb.append(", ");
96                 }
97                 String JavaDoc next = (String JavaDoc) 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     /**
117      * Returns the input stream from which the user input should be read.
118      * @return the input stream from which the user input should be read.
119      */

120     protected InputStream JavaDoc getInputStream() {
121         return System.in;
122     }
123
124 }
125
Popular Tags