View Javadoc

1   /*
2    * NOTE - This file has been modified from the original Ant Version.
3    * The Apache licence under which Ant is released is set out below.
4    */
5   
6   /*
7    * Copyright  2001-2004 The Apache Software Foundation
8    *
9    *  Licensed under the Apache License, Version 2.0 (the "License");
10   *  you may not use this file except in compliance with the License.
11   *  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing, software
16   *  distributed under the License is distributed on an "AS IS" BASIS,
17   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   *  See the License for the specific language governing permissions and
19   *  limitations under the License.
20   *
21   */
22  
23  package org.apache.tools.ant.taskdefs;
24  
25  import java.util.Vector;
26  import org.apache.tools.ant.BuildException;
27  import org.apache.tools.ant.Task;
28  import org.apache.tools.ant.input.InputRequest;
29  import org.apache.tools.ant.input.MultipleChoiceInputRequest;
30  import org.apache.tools.ant.util.StringUtils;
31  
32  import uk.ac.roe.antigen.utils.Config;
33  
34  /***
35   * Reads an input line from the console.
36   * Modified for Antigen from the original Ant version
37   *
38   * @since Ant 1.5
39   *
40   * @ant.task category="control"
41   */
42  public class Input extends Task {
43  
44      private String validargs = null;
45      private String message = "";
46      private String addproperty = null;
47      private String defaultvalue = null;
48  
49      /***
50       * Defines valid input parameters as comma separated strings. If set, input
51       * task will reject any input not defined as accepted and requires the user
52       * to reenter it. Validargs are case sensitive. If you want 'a' and 'A' to
53       * be accepted you need to define both values as accepted arguments.
54       *
55       * @param validargs A comma separated String defining valid input args.
56       */
57      public void setValidargs (String validargs) {
58          this.validargs = validargs;
59      }
60  
61      /***
62       * Defines the name of a property to be created from input. Behaviour is
63       * according to property task which means that existing properties
64       * cannot be overridden.
65       *
66       * @param addproperty Name for the property to be created from input
67       */
68      public void setAddproperty (String addproperty) {
69          this.addproperty = addproperty;
70      }
71  
72      /***
73       * Sets the Message which gets displayed to the user during the build run.
74       * @param message The message to be displayed.
75       */
76      public void setMessage (String message) {
77          this.message = message;
78      }
79  
80      /***
81       * Defines the default value of the property to be created from input.
82       * Property value will be set to default if not input is received.
83       *
84       * @param defaultvalue Default value for the property if no input
85       * is received
86       */
87      public void setDefaultvalue (String defaultvalue) {
88          this.defaultvalue = defaultvalue;
89      }
90  
91      /***
92       * Set a multiline message.
93       * @param msg The message to be displayed.
94       */
95      public void addText(String msg) {
96          message += getProject().replaceProperties(msg);
97      }
98  
99      /***
100      * No arg constructor.
101      */
102     public Input () {
103     }
104 
105     /***
106      * Actual method executed by ant.
107      * @throws BuildException
108      */
109     public void execute () throws BuildException {
110         if (addproperty != null
111             && getProject().getProperty(addproperty) != null) {
112             log("skipping " + getTaskName() + " as property " + addproperty
113                 + " has already been set.");
114             return;
115         }
116 
117         InputRequest request = null;
118         if (validargs != null) {
119             Vector accept = StringUtils.split(validargs, ',');
120             request = new MultipleChoiceInputRequest(message,  accept, defaultvalue);
121         } else {
122             request = new InputRequest(message, defaultvalue, addproperty);
123         }
124 
125         getProject().getInputHandler().handleInput(request);
126 
127         String value = request.getInput();
128         if ((value == null || value.trim().length() == 0)
129             && defaultvalue != null) {
130             value = defaultvalue;
131         }
132         if (addproperty != null && value != null) {
133             getProject().setNewProperty(addproperty, value);
134         }
135     }
136 
137 }