View Javadoc

1   /*
2    * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
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 edu.internet2.middleware.ant.input;
18  
19  import java.util.Vector;
20  
21  import org.apache.tools.ant.BuildException;
22  import org.apache.tools.ant.Task;
23  import org.apache.tools.ant.input.InputRequest;
24  import org.apache.tools.ant.taskdefs.condition.Condition;
25  
26  /**
27   * Extended version of <code>org.apache.tools.ant.taskdefs.Input</code>
28   */
29  public class XInput extends Task implements Condition {
30  
31      private Boolean caseSensitive = new Boolean(true);
32  
33      private String validArgs = null;
34  
35      private String message = "";
36  
37      private String addproperty = null;
38  
39      private String defaultValue = null;
40  
41      private String type = "standard";
42  
43      private Vector options = new Vector();
44  
45      private String value = null;
46  
47      public void setCaseSensitive(Boolean b) {
48  
49          this.caseSensitive = b;
50      }
51  
52      public Boolean getCaseSensitive() {
53  
54          return this.caseSensitive;
55      }
56  
57      /**
58       * Defines valid input parameters as comma separated strings. If set, input task will reject any input not defined
59       * as accepted and requires the user to reenter it. Validargs are case sensitive. If you want 'a' and 'A' to be
60       * accepted you need to define both values as accepted arguments.
61       * 
62       * @param validargs A comma separated String defining valid input args.
63       */
64      public void setValidargs(String validArgs) {
65  
66          this.validArgs = validArgs;
67      }
68  
69      /**
70       * Defines the name of a property to be created from input. Behaviour is according to property task which means that
71       * existing properties cannot be overridden.
72       * 
73       * @param addproperty Name for the property to be created from input
74       */
75      public void setAddproperty(String addproperty) {
76  
77          this.addproperty = addproperty;
78      }
79  
80      /**
81       * Sets the Message which gets displayed to the user during the build run.
82       * 
83       * @param message The message to be displayed.
84       */
85      public void setMessage(String message) {
86  
87          this.message = message;
88      }
89  
90      /**
91       * Defines the default value of the property to be created from input. Property value will be set to default if not
92       * input is received.
93       * 
94       * @param defaultvalue Default value for the property if no input is received
95       */
96      public void setDefaultvalue(String defaultValue) {
97  
98          this.defaultValue = defaultValue;
99      }
100 
101     public void setType(String type) {
102 
103         this.type = type;
104     }
105 
106     public void addConfiguredXoption(XInputOption option) {
107 
108         if (option.getCaseSensitive() == null) {
109             option.setCaseSensitive(getCaseSensitive());
110         }
111         if (defaultValue != null && option.acceptsInput(defaultValue)) {
112             option.setIsDefault(true);
113         }
114 
115         options.add(option);
116     }
117 
118     /**
119      * Set a multiline message.
120      * 
121      * @param msg The message to be displayed.
122      */
123     public void addText(String msg) {
124 
125         message += getProject().replaceProperties(msg);
126     }
127 
128     /**
129      * No arg constructor.
130      */
131     public XInput() {
132 
133     }
134 
135     /**
136      * Actual method executed by ant.
137      * 
138      * @throws BuildException
139      */
140     public void execute() throws BuildException {
141 
142         if (addproperty != null && getProject().getProperty(addproperty) != null) {
143             log("skipping " + getTaskName() + " as property " + addproperty + " has already been set.");
144             return;
145         }
146 
147         InputRequest request = null;
148 
149         if (type.equals("menu")) {
150             getProject().setInputHandler(new XMenuInputHandler());
151             request = new XMultipleChoiceInputRequest(message.trim(), options);
152         } else if (type.equals("confirm")) {
153             setCaseSensitive(new Boolean(false));
154             addConfiguredXoption(new XInputOption("y", "y,yes,t,true", "y"));
155             addConfiguredXoption(new XInputOption("n", "n,no,f,false", "n"));
156 
157             getProject().setInputHandler(new XInputHandler());
158             request = new XMultipleChoiceInputRequest(message.trim(), options);
159         } else {
160             getProject().setInputHandler(new XInputHandler());
161             request = new XMultipleChoiceInputRequest(message.trim(), options);
162         }
163 
164         getProject().getInputHandler().handleInput(request);
165 
166         value = request.getInput();
167         if ((value == null || value.trim().length() == 0) && defaultValue != null) {
168             value = defaultValue;
169         }
170         if (addproperty != null && value != null) {
171             getProject().setNewProperty(addproperty, value);
172         }
173     }
174 
175     public boolean eval() {
176 
177         if (!type.equals("confirm")) {
178             throw new BuildException();
179         }
180 
181         execute();
182         return value.equals("y");
183     }
184 
185 }