You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Input.java 8.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.taskdefs;
  19. import java.util.Vector;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.Task;
  22. import org.apache.tools.ant.input.DefaultInputHandler;
  23. import org.apache.tools.ant.input.GreedyInputHandler;
  24. import org.apache.tools.ant.input.InputHandler;
  25. import org.apache.tools.ant.input.InputRequest;
  26. import org.apache.tools.ant.input.MultipleChoiceInputRequest;
  27. import org.apache.tools.ant.input.PropertyFileInputHandler;
  28. import org.apache.tools.ant.types.EnumeratedAttribute;
  29. import org.apache.tools.ant.util.ClasspathUtils;
  30. import org.apache.tools.ant.util.StringUtils;
  31. /**
  32. * Reads an input line from the console.
  33. *
  34. * @since Ant 1.5
  35. *
  36. * @ant.task category="control"
  37. */
  38. public class Input extends Task {
  39. /**
  40. * Represents an InputHandler.
  41. */
  42. public class Handler extends DefBase {
  43. private String refid = null;
  44. private HandlerType type = null;
  45. private String classname = null;
  46. /**
  47. * Specify that the handler is a reference on the project;
  48. * this allows the use of a custom inputhandler.
  49. * @param refid the String refid.
  50. */
  51. public void setRefid(String refid) {
  52. this.refid = refid;
  53. }
  54. /**
  55. * Get the refid of this Handler.
  56. * @return String refid.
  57. */
  58. public String getRefid() {
  59. return refid;
  60. }
  61. /**
  62. * Set the InputHandler classname.
  63. * @param classname the String classname.
  64. */
  65. public void setClassname(String classname) {
  66. this.classname = classname;
  67. }
  68. /**
  69. * Get the classname of the InputHandler.
  70. * @return String classname.
  71. */
  72. public String getClassname() {
  73. return classname;
  74. }
  75. /**
  76. * Set the handler type.
  77. * @param type a HandlerType.
  78. */
  79. public void setType(HandlerType type) {
  80. this.type = type;
  81. }
  82. /**
  83. * Get the handler type.
  84. * @return a HandlerType object.
  85. */
  86. public HandlerType getType() {
  87. return type;
  88. }
  89. private InputHandler getInputHandler() {
  90. if (type != null) {
  91. return type.getInputHandler();
  92. }
  93. if (refid != null) {
  94. try {
  95. return (InputHandler) (getProject().getReference(refid));
  96. } catch (ClassCastException e) {
  97. throw new BuildException(
  98. refid + " does not denote an InputHandler", e);
  99. }
  100. }
  101. if (classname != null) {
  102. return (InputHandler) (ClasspathUtils.newInstance(classname,
  103. createLoader(), InputHandler.class));
  104. }
  105. throw new BuildException(
  106. "Must specify refid, classname or type");
  107. }
  108. }
  109. /**
  110. * EnumeratedAttribute representing the built-in input handler types:
  111. * "default", "propertyfile", "greedy".
  112. */
  113. public static class HandlerType extends EnumeratedAttribute {
  114. private static final String[] VALUES
  115. = {"default", "propertyfile", "greedy"};
  116. private static final InputHandler[] HANDLERS
  117. = {new DefaultInputHandler(),
  118. new PropertyFileInputHandler(),
  119. new GreedyInputHandler()};
  120. //inherit doc
  121. public String[] getValues() {
  122. return VALUES;
  123. }
  124. private InputHandler getInputHandler() {
  125. return HANDLERS[getIndex()];
  126. }
  127. }
  128. private String validargs = null;
  129. private String message = "";
  130. private String addproperty = null;
  131. private String defaultvalue = null;
  132. private Handler handler = null;
  133. private boolean messageAttribute;
  134. /**
  135. * Defines valid input parameters as comma separated strings. If set, input
  136. * task will reject any input not defined as accepted and requires the user
  137. * to reenter it. Validargs are case sensitive. If you want 'a' and 'A' to
  138. * be accepted you need to define both values as accepted arguments.
  139. *
  140. * @param validargs A comma separated String defining valid input args.
  141. */
  142. public void setValidargs (String validargs) {
  143. this.validargs = validargs;
  144. }
  145. /**
  146. * Defines the name of a property to be created from input. Behaviour is
  147. * according to property task which means that existing properties
  148. * cannot be overridden.
  149. *
  150. * @param addproperty Name for the property to be created from input
  151. */
  152. public void setAddproperty (String addproperty) {
  153. this.addproperty = addproperty;
  154. }
  155. /**
  156. * Sets the Message which gets displayed to the user during the build run.
  157. * @param message The message to be displayed.
  158. */
  159. public void setMessage (String message) {
  160. this.message = message;
  161. messageAttribute = true;
  162. }
  163. /**
  164. * Defines the default value of the property to be created from input.
  165. * Property value will be set to default if not input is received.
  166. *
  167. * @param defaultvalue Default value for the property if no input
  168. * is received
  169. */
  170. public void setDefaultvalue (String defaultvalue) {
  171. this.defaultvalue = defaultvalue;
  172. }
  173. /**
  174. * Set a multiline message.
  175. * @param msg The message to be displayed.
  176. */
  177. public void addText(String msg) {
  178. if (messageAttribute && "".equals(msg.trim())) {
  179. return;
  180. }
  181. message += getProject().replaceProperties(msg);
  182. }
  183. /**
  184. * No arg constructor.
  185. */
  186. public Input () {
  187. }
  188. /**
  189. * Actual method executed by ant.
  190. * @throws BuildException on error
  191. */
  192. public void execute () throws BuildException {
  193. if (addproperty != null
  194. && getProject().getProperty(addproperty) != null) {
  195. log("skipping " + getTaskName() + " as property " + addproperty
  196. + " has already been set.");
  197. return;
  198. }
  199. InputRequest request = null;
  200. if (validargs != null) {
  201. Vector accept = StringUtils.split(validargs, ',');
  202. request = new MultipleChoiceInputRequest(message, accept);
  203. } else {
  204. request = new InputRequest(message);
  205. }
  206. request.setDefaultValue(defaultvalue);
  207. InputHandler h = handler == null
  208. ? getProject().getInputHandler()
  209. : handler.getInputHandler();
  210. h.handleInput(request);
  211. String value = request.getInput();
  212. if ((value == null || value.trim().length() == 0)
  213. && defaultvalue != null) {
  214. value = defaultvalue;
  215. }
  216. if (addproperty != null && value != null) {
  217. getProject().setNewProperty(addproperty, value);
  218. }
  219. }
  220. /**
  221. * Create a nested handler element.
  222. * @return a Handler for this Input task.
  223. */
  224. public Handler createHandler() {
  225. if (handler != null) {
  226. throw new BuildException(
  227. "Cannot define > 1 nested input handler");
  228. }
  229. handler = new Handler();
  230. return handler;
  231. }
  232. }