1 | /*
|
---|
2 | * Copyright 2002-2005 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.input;
|
---|
19 |
|
---|
20 | import java.io.DataInputStream;
|
---|
21 | import java.io.IOException;
|
---|
22 | import java.io.InputStream;
|
---|
23 | import java.util.Enumeration;
|
---|
24 | import org.apache.tools.ant.BuildException;
|
---|
25 | import org.apache.tools.ant.util.KeepAliveInputStream;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Prompts on System.err, reads input from System.in
|
---|
29 | *
|
---|
30 | * @since Ant 1.5
|
---|
31 | */
|
---|
32 | public class DefaultInputHandler implements InputHandler {
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Empty no-arg constructor
|
---|
36 | */
|
---|
37 | public DefaultInputHandler() {
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Prompts and requests input. May loop until a valid input has
|
---|
42 | * been entered.
|
---|
43 | * @param request the request to handle
|
---|
44 | * @throws BuildException if not possible to read from console
|
---|
45 | */
|
---|
46 | public void handleInput(InputRequest request) throws BuildException {
|
---|
47 | String prompt = getPrompt(request);
|
---|
48 | DataInputStream in = null;
|
---|
49 | try {
|
---|
50 | in =
|
---|
51 | new DataInputStream(new KeepAliveInputStream(getInputStream()));
|
---|
52 | do {
|
---|
53 | System.err.println(prompt);
|
---|
54 | System.err.flush();
|
---|
55 | try {
|
---|
56 | String input = in.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 (in != null) {
|
---|
65 | try {
|
---|
66 | in.close();
|
---|
67 | } catch (IOException 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 getPrompt(InputRequest request) {
|
---|
85 | String prompt = request.getPrompt();
|
---|
86 | if (request instanceof MultipleChoiceInputRequest) {
|
---|
87 | StringBuffer sb = new StringBuffer(prompt);
|
---|
88 | sb.append("(");
|
---|
89 | Enumeration e =
|
---|
90 | ((MultipleChoiceInputRequest) request).getChoices().elements();
|
---|
91 | boolean first = true;
|
---|
92 | while (e.hasMoreElements()) {
|
---|
93 | if (!first) {
|
---|
94 | sb.append(",");
|
---|
95 | }
|
---|
96 | sb.append(e.nextElement());
|
---|
97 | first = false;
|
---|
98 | }
|
---|
99 | sb.append(")");
|
---|
100 | prompt = sb.toString();
|
---|
101 | }
|
---|
102 | return prompt;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Returns the input stream from which the user input should be read.
|
---|
107 | * @return the input stream from which the user input should be read.
|
---|
108 | */
|
---|
109 | protected InputStream getInputStream() {
|
---|
110 | return System.in;
|
---|
111 | }
|
---|
112 |
|
---|
113 | }
|
---|