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.2 kB

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