1. /*
  2. * Copyright 2002-2004 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. package org.apache.tools.ant.input;
  18. import java.io.DataInputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.util.Enumeration;
  22. import org.apache.tools.ant.BuildException;
  23. import org.apache.tools.ant.util.KeepAliveInputStream;
  24. /**
  25. * Prompts on System.err, reads input from System.in
  26. *
  27. * @version $Revision: 1.12.2.5 $
  28. * @since Ant 1.5
  29. */
  30. public class DefaultInputHandler implements InputHandler {
  31. /**
  32. * Empty no-arg constructor
  33. */
  34. public DefaultInputHandler() {
  35. }
  36. /**
  37. * Prompts and requests input. May loop until a valid input has
  38. * been entered.
  39. * @param request the request to handle
  40. * @throws BuildException if not possible to read from console
  41. */
  42. public void handleInput(InputRequest request) throws BuildException {
  43. String prompt = getPrompt(request);
  44. DataInputStream in = null;
  45. try {
  46. in =
  47. new DataInputStream(new KeepAliveInputStream(getInputStream()));
  48. do {
  49. System.err.println(prompt);
  50. System.err.flush();
  51. try {
  52. String input = in.readLine();
  53. request.setInput(input);
  54. } catch (IOException e) {
  55. throw new BuildException("Failed to read input from"
  56. + " Console.", e);
  57. }
  58. } while (!request.isInputValid());
  59. } finally {
  60. if (in != null) {
  61. try {
  62. in.close();
  63. } catch (IOException e) {
  64. throw new BuildException("Failed to close input.", e);
  65. }
  66. }
  67. }
  68. }
  69. /**
  70. * Constructs user prompt from a request.
  71. *
  72. * <p>This implementation adds (choice1,choice2,choice3,...) to the
  73. * prompt for <code>MultipleChoiceInputRequest</code>s.</p>
  74. *
  75. * @param request the request to construct the prompt for.
  76. * Must not be <code>null</code>.
  77. * @return the prompt to ask the user
  78. */
  79. protected String getPrompt(InputRequest request) {
  80. String prompt = request.getPrompt();
  81. if (request instanceof MultipleChoiceInputRequest) {
  82. StringBuffer sb = new StringBuffer(prompt);
  83. sb.append("(");
  84. Enumeration e =
  85. ((MultipleChoiceInputRequest) request).getChoices().elements();
  86. boolean first = true;
  87. while (e.hasMoreElements()) {
  88. if (!first) {
  89. sb.append(",");
  90. }
  91. sb.append(e.nextElement());
  92. first = false;
  93. }
  94. sb.append(")");
  95. prompt = sb.toString();
  96. }
  97. return prompt;
  98. }
  99. /**
  100. * Returns the input stream from which the user input should be read.
  101. * @return the input stream from which the user input should be read.
  102. */
  103. protected InputStream getInputStream() {
  104. return System.in;
  105. }
  106. }