1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
59
60
61
62
63
64 public void setValidargs(String validArgs) {
65
66 this.validArgs = validArgs;
67 }
68
69
70
71
72
73
74
75 public void setAddproperty(String addproperty) {
76
77 this.addproperty = addproperty;
78 }
79
80
81
82
83
84
85 public void setMessage(String message) {
86
87 this.message = message;
88 }
89
90
91
92
93
94
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
120
121
122
123 public void addText(String msg) {
124
125 message += getProject().replaceProperties(msg);
126 }
127
128
129
130
131 public XInput() {
132
133 }
134
135
136
137
138
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 }