1. /*
  2. * Copyright 2001-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.taskdefs;
  18. import java.util.Vector;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.Task;
  21. import org.apache.tools.ant.input.InputRequest;
  22. import org.apache.tools.ant.input.MultipleChoiceInputRequest;
  23. import org.apache.tools.ant.util.StringUtils;
  24. /**
  25. * Reads an input line from the console.
  26. *
  27. *
  28. * @since Ant 1.5
  29. *
  30. * @ant.task category="control"
  31. */
  32. public class Input extends Task {
  33. private String validargs = null;
  34. private String message = "";
  35. private String addproperty = null;
  36. private String defaultvalue = null;
  37. /**
  38. * Defines valid input parameters as comma separated strings. If set, input
  39. * task will reject any input not defined as accepted and requires the user
  40. * to reenter it. Validargs are case sensitive. If you want 'a' and 'A' to
  41. * be accepted you need to define both values as accepted arguments.
  42. *
  43. * @param validargs A comma separated String defining valid input args.
  44. */
  45. public void setValidargs (String validargs) {
  46. this.validargs = validargs;
  47. }
  48. /**
  49. * Defines the name of a property to be created from input. Behaviour is
  50. * according to property task which means that existing properties
  51. * cannot be overridden.
  52. *
  53. * @param addproperty Name for the property to be created from input
  54. */
  55. public void setAddproperty (String addproperty) {
  56. this.addproperty = addproperty;
  57. }
  58. /**
  59. * Sets the Message which gets displayed to the user during the build run.
  60. * @param message The message to be displayed.
  61. */
  62. public void setMessage (String message) {
  63. this.message = message;
  64. }
  65. /**
  66. * Defines the default value of the property to be created from input.
  67. * Property value will be set to default if not input is received.
  68. *
  69. * @param defaultvalue Default value for the property if no input
  70. * is received
  71. */
  72. public void setDefaultvalue (String defaultvalue) {
  73. this.defaultvalue = defaultvalue;
  74. }
  75. /**
  76. * Set a multiline message.
  77. * @param msg The message to be displayed.
  78. */
  79. public void addText(String msg) {
  80. message += getProject().replaceProperties(msg);
  81. }
  82. /**
  83. * No arg constructor.
  84. */
  85. public Input () {
  86. }
  87. /**
  88. * Actual method executed by ant.
  89. * @throws BuildException
  90. */
  91. public void execute () throws BuildException {
  92. if (addproperty != null
  93. && getProject().getProperty(addproperty) != null) {
  94. log("skipping " + getTaskName() + " as property " + addproperty
  95. + " has already been set.");
  96. return;
  97. }
  98. InputRequest request = null;
  99. if (validargs != null) {
  100. Vector accept = StringUtils.split(validargs, ',');
  101. request = new MultipleChoiceInputRequest(message, accept);
  102. } else {
  103. request = new InputRequest(message);
  104. }
  105. getProject().getInputHandler().handleInput(request);
  106. String value = request.getInput();
  107. if ((value == null || value.trim().length() == 0)
  108. && defaultvalue != null) {
  109. value = defaultvalue;
  110. }
  111. if (addproperty != null && value != null) {
  112. getProject().setNewProperty(addproperty, value);
  113. }
  114. }
  115. }