FileDocCategorySizeDatePackage
DefaultInputHandler.javaAPI DocApache Ant 1.704169Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.input

DefaultInputHandler

public class DefaultInputHandler extends Object implements InputHandler
Prompts on System.err, reads input from System.in
since
Ant 1.5

Fields Summary
Constructors Summary
public DefaultInputHandler()
Empty no-arg constructor

    
Methods Summary
protected java.io.InputStreamgetInputStream()
Returns the input stream from which the user input should be read.

return
the input stream from which the user input should be read.

        return System.in;
    
protected java.lang.StringgetPrompt(InputRequest request)
Constructs user prompt from a request.

This implementation adds (choice1,choice2,choice3,...) to the prompt for MultipleChoiceInputRequests.

param
request the request to construct the prompt for. Must not be null.
return
the prompt to ask the user

        String prompt = request.getPrompt();
        String def = request.getDefaultValue();
        if (request instanceof MultipleChoiceInputRequest) {
            StringBuffer sb = new StringBuffer(prompt);
            sb.append(" (");
            Enumeration e =
                ((MultipleChoiceInputRequest) request).getChoices().elements();
            boolean first = true;
            while (e.hasMoreElements()) {
                if (!first) {
                    sb.append(", ");
                }
                String next = (String) e.nextElement();
                if (next.equals(def)) {
                    sb.append('[");
                }
                sb.append(next);
                if (next.equals(def)) {
                    sb.append(']");
                }
                first = false;
            }
            sb.append(")");
            return sb.toString();
        } else if (def != null) {
            return prompt + " [" + def + "]";
        } else {
            return prompt;
        }
    
public voidhandleInput(InputRequest request)
Prompts and requests input. May loop until a valid input has been entered.

param
request the request to handle
throws
BuildException if not possible to read from console

        String prompt = getPrompt(request);
        BufferedReader r = null;
        try {
            r = new BufferedReader(new InputStreamReader(getInputStream()));
            do {
                System.err.println(prompt);
                System.err.flush();
                try {
                    String input = r.readLine();
                    request.setInput(input);
                } catch (IOException e) {
                    throw new BuildException("Failed to read input from"
                                             + " Console.", e);
                }
            } while (!request.isInputValid());
        } finally {
            if (r != null) {
                try {
                    r.close();
                } catch (IOException e) {
                    throw new BuildException("Failed to close input.", e);
                }
            }
        }