001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.input;
020:
021: import java.io.BufferedReader;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.InputStreamReader;
025: import java.util.Enumeration;
026: import org.apache.tools.ant.BuildException;
027:
028: /**
029: * Prompts on System.err, reads input from System.in
030: *
031: * @since Ant 1.5
032: */
033: public class DefaultInputHandler implements InputHandler {
034:
035: /**
036: * Empty no-arg constructor
037: */
038: public DefaultInputHandler() {
039: }
040:
041: /**
042: * Prompts and requests input. May loop until a valid input has
043: * been entered.
044: * @param request the request to handle
045: * @throws BuildException if not possible to read from console
046: */
047: public void handleInput(InputRequest request) throws BuildException {
048: String prompt = getPrompt(request);
049: BufferedReader r = null;
050: try {
051: r = new BufferedReader(new InputStreamReader(
052: getInputStream()));
053: do {
054: System.err.println(prompt);
055: System.err.flush();
056: try {
057: String input = r.readLine();
058: request.setInput(input);
059: } catch (IOException e) {
060: throw new BuildException(
061: "Failed to read input from" + " Console.",
062: e);
063: }
064: } while (!request.isInputValid());
065: } finally {
066: if (r != null) {
067: try {
068: r.close();
069: } catch (IOException e) {
070: throw new BuildException("Failed to close input.",
071: e);
072: }
073: }
074: }
075: }
076:
077: /**
078: * Constructs user prompt from a request.
079: *
080: * <p>This implementation adds (choice1,choice2,choice3,...) to the
081: * prompt for <code>MultipleChoiceInputRequest</code>s.</p>
082: *
083: * @param request the request to construct the prompt for.
084: * Must not be <code>null</code>.
085: * @return the prompt to ask the user
086: */
087: protected String getPrompt(InputRequest request) {
088: String prompt = request.getPrompt();
089: String def = request.getDefaultValue();
090: if (request instanceof MultipleChoiceInputRequest) {
091: StringBuffer sb = new StringBuffer(prompt);
092: sb.append(" (");
093: Enumeration e = ((MultipleChoiceInputRequest) request)
094: .getChoices().elements();
095: boolean first = true;
096: while (e.hasMoreElements()) {
097: if (!first) {
098: sb.append(", ");
099: }
100: String next = (String) e.nextElement();
101: if (next.equals(def)) {
102: sb.append('[');
103: }
104: sb.append(next);
105: if (next.equals(def)) {
106: sb.append(']');
107: }
108: first = false;
109: }
110: sb.append(")");
111: return sb.toString();
112: } else if (def != null) {
113: return prompt + " [" + def + "]";
114: } else {
115: return prompt;
116: }
117: }
118:
119: /**
120: * Returns the input stream from which the user input should be read.
121: * @return the input stream from which the user input should be read.
122: */
123: protected InputStream getInputStream() {
124: return System.in;
125: }
126:
127: }
|