KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > Input


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
19 package org.apache.tools.ant.taskdefs;
20
21 import java.util.Vector JavaDoc;
22
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.Task;
25 import org.apache.tools.ant.input.DefaultInputHandler;
26 import org.apache.tools.ant.input.GreedyInputHandler;
27 import org.apache.tools.ant.input.InputHandler;
28 import org.apache.tools.ant.input.InputRequest;
29 import org.apache.tools.ant.input.MultipleChoiceInputRequest;
30 import org.apache.tools.ant.input.PropertyFileInputHandler;
31 import org.apache.tools.ant.types.EnumeratedAttribute;
32 import org.apache.tools.ant.util.ClasspathUtils;
33 import org.apache.tools.ant.util.StringUtils;
34
35 /**
36  * Reads an input line from the console.
37  *
38  * @since Ant 1.5
39  *
40  * @ant.task category="control"
41  */

42 public class Input extends Task {
43
44     /**
45      * Represents an InputHandler.
46      */

47     public class Handler extends DefBase {
48
49         private String JavaDoc refid = null;
50         private HandlerType type = null;
51         private String JavaDoc classname = null;
52
53         /**
54          * Specify that the handler is a reference on the project;
55          * this allows the use of a custom inputhandler.
56          * @param refid the String refid.
57          */

58         public void setRefid(String JavaDoc refid) {
59             this.refid = refid;
60         }
61         /**
62          * Get the refid of this Handler.
63          * @return String refid.
64          */

65         public String JavaDoc getRefid() {
66             return refid;
67         }
68         /**
69          * Set the InputHandler classname.
70          * @param classname the String classname.
71          */

72         public void setClassname(String JavaDoc classname) {
73             this.classname = classname;
74         }
75         /**
76          * Get the classname of the InputHandler.
77          * @return String classname.
78          */

79         public String JavaDoc getClassname() {
80             return classname;
81         }
82         /**
83          * Set the handler type.
84          * @param type a HandlerType.
85          */

86         public void setType(HandlerType type) {
87             this.type = type;
88         }
89         /**
90          * Get the handler type.
91          * @return a HandlerType object.
92          */

93         public HandlerType getType() {
94             return type;
95         }
96         private InputHandler getInputHandler() {
97             if (type != null) {
98                return type.getInputHandler();
99             }
100             if (refid != null) {
101                try {
102                    return (InputHandler) (getProject().getReference(refid));
103                } catch (ClassCastException JavaDoc e) {
104                    throw new BuildException(
105                        refid + " does not denote an InputHandler", e);
106                }
107             }
108             if (classname != null) {
109                return (InputHandler) (ClasspathUtils.newInstance(classname,
110                    createLoader(), InputHandler.class));
111             }
112             throw new BuildException(
113                 "Must specify refid, classname or type");
114         }
115     }
116
117     /**
118      * EnumeratedAttribute representing the built-in input handler types:
119      * "default", "propertyfile", "greedy".
120      */

121     public static class HandlerType extends EnumeratedAttribute {
122         private static final String JavaDoc[] VALUES
123             = {"default", "propertyfile", "greedy"};
124
125         private static final InputHandler[] HANDLERS
126             = {new DefaultInputHandler(),
127                new PropertyFileInputHandler(),
128                new GreedyInputHandler()};
129
130         /** {@inheritDoc} */
131         public String JavaDoc[] getValues() {
132             return VALUES;
133         }
134         private InputHandler getInputHandler() {
135             return HANDLERS[getIndex()];
136         }
137     }
138
139     private String JavaDoc validargs = null;
140     private String JavaDoc message = "";
141     private String JavaDoc addproperty = null;
142     private String JavaDoc defaultvalue = null;
143     private Handler handler = null;
144     private boolean messageAttribute;
145
146     /**
147      * Defines valid input parameters as comma separated strings. If set, input
148      * task will reject any input not defined as accepted and requires the user
149      * to reenter it. Validargs are case sensitive. If you want 'a' and 'A' to
150      * be accepted you need to define both values as accepted arguments.
151      *
152      * @param validargs A comma separated String defining valid input args.
153      */

154     public void setValidargs (String JavaDoc validargs) {
155         this.validargs = validargs;
156     }
157
158     /**
159      * Defines the name of a property to be created from input. Behaviour is
160      * according to property task which means that existing properties
161      * cannot be overridden.
162      *
163      * @param addproperty Name for the property to be created from input
164      */

165     public void setAddproperty (String JavaDoc addproperty) {
166         this.addproperty = addproperty;
167     }
168
169     /**
170      * Sets the Message which gets displayed to the user during the build run.
171      * @param message The message to be displayed.
172      */

173     public void setMessage (String JavaDoc message) {
174         this.message = message;
175         messageAttribute = true;
176     }
177
178     /**
179      * Defines the default value of the property to be created from input.
180      * Property value will be set to default if not input is received.
181      *
182      * @param defaultvalue Default value for the property if no input
183      * is received
184      */

185     public void setDefaultvalue (String JavaDoc defaultvalue) {
186         this.defaultvalue = defaultvalue;
187     }
188
189     /**
190      * Set a multiline message.
191      * @param msg The message to be displayed.
192      */

193     public void addText(String JavaDoc msg) {
194         if (messageAttribute && "".equals(msg.trim())) {
195             return;
196         }
197         message += getProject().replaceProperties(msg);
198     }
199
200     /**
201      * No arg constructor.
202      */

203     public Input () {
204     }
205
206     /**
207      * Actual method executed by ant.
208      * @throws BuildException on error
209      */

210     public void execute () throws BuildException {
211         if (addproperty != null
212             && getProject().getProperty(addproperty) != null) {
213             log("skipping " + getTaskName() + " as property " + addproperty
214                 + " has already been set.");
215             return;
216         }
217
218         InputRequest request = null;
219         if (validargs != null) {
220             Vector JavaDoc accept = StringUtils.split(validargs, ',');
221             request = new MultipleChoiceInputRequest(message, accept);
222         } else {
223             request = new InputRequest(message);
224         }
225         request.setDefaultValue(defaultvalue);
226
227         InputHandler h = handler == null
228             ? getProject().getInputHandler()
229             : handler.getInputHandler();
230
231         h.handleInput(request);
232
233         String JavaDoc value = request.getInput();
234         if ((value == null || value.trim().length() == 0)
235             && defaultvalue != null) {
236             value = defaultvalue;
237         }
238         if (addproperty != null && value != null) {
239             getProject().setNewProperty(addproperty, value);
240         }
241     }
242
243     /**
244      * Create a nested handler element.
245      * @return a Handler for this Input task.
246      */

247     public Handler createHandler() {
248         if (handler != null) {
249             throw new BuildException(
250                 "Cannot define > 1 nested input handler");
251         }
252         handler = new Handler();
253         return handler;
254     }
255
256 }
257
Popular Tags