1 | /*
|
---|
2 | * Copyright 2001-2004 The Apache Software Foundation
|
---|
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 |
|
---|
18 | package org.apache.tools.ant.taskdefs;
|
---|
19 |
|
---|
20 | import java.util.Vector;
|
---|
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.input.MultipleChoiceInputRequest;
|
---|
25 | import org.apache.tools.ant.util.StringUtils;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Reads an input line from the console.
|
---|
29 | *
|
---|
30 | *
|
---|
31 | * @since Ant 1.5
|
---|
32 | *
|
---|
33 | * @ant.task category="control"
|
---|
34 | */
|
---|
35 | public class Input extends Task {
|
---|
36 | private String validargs = null;
|
---|
37 | private String message = "";
|
---|
38 | private String addproperty = null;
|
---|
39 | private String defaultvalue = null;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Defines valid input parameters as comma separated strings. If set, input
|
---|
43 | * task will reject any input not defined as accepted and requires the user
|
---|
44 | * to reenter it. Validargs are case sensitive. If you want 'a' and 'A' to
|
---|
45 | * be accepted you need to define both values as accepted arguments.
|
---|
46 | *
|
---|
47 | * @param validargs A comma separated String defining valid input args.
|
---|
48 | */
|
---|
49 | public void setValidargs (String validargs) {
|
---|
50 | this.validargs = validargs;
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Defines the name of a property to be created from input. Behaviour is
|
---|
55 | * according to property task which means that existing properties
|
---|
56 | * cannot be overridden.
|
---|
57 | *
|
---|
58 | * @param addproperty Name for the property to be created from input
|
---|
59 | */
|
---|
60 | public void setAddproperty (String addproperty) {
|
---|
61 | this.addproperty = addproperty;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Sets the Message which gets displayed to the user during the build run.
|
---|
66 | * @param message The message to be displayed.
|
---|
67 | */
|
---|
68 | public void setMessage (String message) {
|
---|
69 | this.message = message;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Defines the default value of the property to be created from input.
|
---|
74 | * Property value will be set to default if not input is received.
|
---|
75 | *
|
---|
76 | * @param defaultvalue Default value for the property if no input
|
---|
77 | * is received
|
---|
78 | */
|
---|
79 | public void setDefaultvalue (String defaultvalue) {
|
---|
80 | this.defaultvalue = defaultvalue;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Set a multiline message.
|
---|
85 | * @param msg The message to be displayed.
|
---|
86 | */
|
---|
87 | public void addText(String msg) {
|
---|
88 | message += getProject().replaceProperties(msg);
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * No arg constructor.
|
---|
93 | */
|
---|
94 | public Input () {
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Actual method executed by ant.
|
---|
99 | * @throws BuildException on error
|
---|
100 | */
|
---|
101 | public void execute () throws BuildException {
|
---|
102 | if (addproperty != null
|
---|
103 | && getProject().getProperty(addproperty) != null) {
|
---|
104 | log("skipping " + getTaskName() + " as property " + addproperty
|
---|
105 | + " has already been set.");
|
---|
106 | return;
|
---|
107 | }
|
---|
108 |
|
---|
109 | InputRequest request = null;
|
---|
110 | if (validargs != null) {
|
---|
111 | Vector accept = StringUtils.split(validargs, ',');
|
---|
112 | request = new MultipleChoiceInputRequest(message, accept);
|
---|
113 | } else {
|
---|
114 | request = new InputRequest(message);
|
---|
115 | }
|
---|
116 |
|
---|
117 | getProject().getInputHandler().handleInput(request);
|
---|
118 |
|
---|
119 | String value = request.getInput();
|
---|
120 | if ((value == null || value.trim().length() == 0)
|
---|
121 | && defaultvalue != null) {
|
---|
122 | value = defaultvalue;
|
---|
123 | }
|
---|
124 | if (addproperty != null && value != null) {
|
---|
125 | getProject().setNewProperty(addproperty, value);
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | }
|
---|