1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.util.Vector ; 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 42 public class Input extends Task { 43 44 47 public class Handler extends DefBase { 48 49 private String refid = null; 50 private HandlerType type = null; 51 private String classname = null; 52 53 58 public void setRefid(String refid) { 59 this.refid = refid; 60 } 61 65 public String getRefid() { 66 return refid; 67 } 68 72 public void setClassname(String classname) { 73 this.classname = classname; 74 } 75 79 public String getClassname() { 80 return classname; 81 } 82 86 public void setType(HandlerType type) { 87 this.type = type; 88 } 89 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 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 121 public static class HandlerType extends EnumeratedAttribute { 122 private static final String [] VALUES 123 = {"default", "propertyfile", "greedy"}; 124 125 private static final InputHandler[] HANDLERS 126 = {new DefaultInputHandler(), 127 new PropertyFileInputHandler(), 128 new GreedyInputHandler()}; 129 130 131 public String [] getValues() { 132 return VALUES; 133 } 134 private InputHandler getInputHandler() { 135 return HANDLERS[getIndex()]; 136 } 137 } 138 139 private String validargs = null; 140 private String message = ""; 141 private String addproperty = null; 142 private String defaultvalue = null; 143 private Handler handler = null; 144 private boolean messageAttribute; 145 146 154 public void setValidargs (String validargs) { 155 this.validargs = validargs; 156 } 157 158 165 public void setAddproperty (String addproperty) { 166 this.addproperty = addproperty; 167 } 168 169 173 public void setMessage (String message) { 174 this.message = message; 175 messageAttribute = true; 176 } 177 178 185 public void setDefaultvalue (String defaultvalue) { 186 this.defaultvalue = defaultvalue; 187 } 188 189 193 public void addText(String msg) { 194 if (messageAttribute && "".equals(msg.trim())) { 195 return; 196 } 197 message += getProject().replaceProperties(msg); 198 } 199 200 203 public Input () { 204 } 205 206 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 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 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 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 |