git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272541 13f79535-47bb-0310-9956-ffa450edef68master
@@ -0,0 +1,4 @@ | |||
Press\ Return\ key\ to\ continue...=test | |||
All\ data\ is\ going\ to\ be\ deleted\ from\ DB\ continue?=test | |||
All\ data\ is\ going\ to\ be\ deleted\ from\ db\ continue\ (y/n)?=y | |||
Please\ enter\ db-username\:=scott |
@@ -3,32 +3,27 @@ | |||
<project name="input-test" basedir="." default="test1"> | |||
<target name="test1"> | |||
<input testinput="test">Press Return key to continue...</input> | |||
<input>Press Return key to continue...</input> | |||
</target> | |||
<target name="test2"> | |||
<input testinput="test" | |||
message="Press Return key to continue..." | |||
/> | |||
<input message="Press Return key to continue..." /> | |||
</target> | |||
<target name="test3"> | |||
<input testinput="test" | |||
message="All data is going to be deleted from DB continue (y/n)?" | |||
<input message="All data is going to be deleted from DB continue?" | |||
validargs="y,n" | |||
/> | |||
</target> | |||
<target name="test5"> | |||
<input testinput="y" | |||
message="All data is going to be deleted from DB continue (y/n)?" | |||
<input message="All data is going to be deleted from db continue (y/n)?" | |||
validargs="y,n" | |||
/> | |||
</target> | |||
<target name="test6"> | |||
<input testinput="scott" | |||
message="Please enter db-username:" | |||
<input message="Please enter db-username:" | |||
addproperty="db.user" | |||
/> | |||
</target> | |||
@@ -54,6 +54,9 @@ | |||
package org.apache.tools.ant; | |||
import org.apache.tools.ant.input.DefaultInputHandler; | |||
import org.apache.tools.ant.input.InputHandler; | |||
import java.io.File; | |||
import java.io.FileInputStream; | |||
import java.io.PrintStream; | |||
@@ -111,6 +114,12 @@ public class Main { | |||
* BuildLogger interface. | |||
*/ | |||
private String loggerClassname = null; | |||
/** | |||
* The Ant InputHandler class. There may be only one input | |||
* handler. | |||
*/ | |||
private String inputHandlerClassname = null; | |||
/** | |||
* Whether or not output to the log is to be unadorned. | |||
@@ -339,6 +348,19 @@ public class Main { | |||
"using the -logger argument"); | |||
return; | |||
} | |||
} else if (arg.equals("-inputhandler")) { | |||
if (inputHandlerClassname != null) { | |||
System.out.println("Only one input handler class may " + | |||
"be specified."); | |||
return; | |||
} | |||
try { | |||
inputHandlerClassname = args[++i]; | |||
} catch (ArrayIndexOutOfBoundsException aioobe) { | |||
System.out.println("You must specify a classname when " + | |||
"using the -inputhandler argument"); | |||
return; | |||
} | |||
} else if (arg.equals("-emacs")) { | |||
emacsMode = true; | |||
} else if (arg.equals("-projecthelp")) { | |||
@@ -528,6 +550,7 @@ public class Main { | |||
try { | |||
addBuildListeners(project); | |||
addInputHandler(project); | |||
PrintStream err = System.err; | |||
PrintStream out = System.out; | |||
@@ -627,6 +650,36 @@ public class Main { | |||
} | |||
} | |||
/** | |||
* Creates the InputHandler and adds it to the project. | |||
* | |||
* @exception BuildException if a specified InputHandler | |||
* implementation could not be loaded. | |||
*/ | |||
private void addInputHandler(Project project) { | |||
InputHandler handler = null; | |||
if (inputHandlerClassname == null) { | |||
handler = new DefaultInputHandler(); | |||
} else { | |||
try { | |||
handler = (InputHandler) | |||
(Class.forName(inputHandlerClassname).newInstance()); | |||
} catch (ClassCastException e) { | |||
String msg = "The specified input handler class " | |||
+ inputHandlerClassname | |||
+ " does not implement the InputHandler interface"; | |||
throw new BuildException(msg); | |||
} | |||
catch (Exception e) { | |||
String msg = "Unable to instantiate specified input handler " | |||
+ "class " + inputHandlerClassname + " : " | |||
+ e.getClass().getName(); | |||
throw new BuildException(msg); | |||
} | |||
} | |||
project.setInputHandler(handler); | |||
} | |||
// XXX: (Jon Skeet) Any reason for writing a message and then using a bare | |||
// RuntimeException rather than just using a BuildException here? Is it | |||
// in case the message could end up being written to no loggers (as the | |||
@@ -69,6 +69,7 @@ import org.apache.tools.ant.types.FilterSet; | |||
import org.apache.tools.ant.types.FilterSetCollection; | |||
import org.apache.tools.ant.util.FileUtils; | |||
import org.apache.tools.ant.util.JavaEnvUtils; | |||
import org.apache.tools.ant.input.InputHandler; | |||
/** | |||
* Central representation of an Ant project. This class defines an | |||
@@ -181,6 +182,25 @@ public class Project { | |||
/** Records the latest task to be executed on a thread (Thread to Task). */ | |||
private Hashtable threadTasks = new Hashtable(); | |||
/** | |||
* Called to handle any input requests. | |||
*/ | |||
private InputHandler inputHandler = null; | |||
/** | |||
* Sets the input handler | |||
*/ | |||
public void setInputHandler(InputHandler handler) { | |||
inputHandler = handler; | |||
} | |||
/** | |||
* Retrieves the current input handler. | |||
*/ | |||
public InputHandler getInputHandler() { | |||
return inputHandler; | |||
} | |||
/** Instance of a utility class to use for file operations. */ | |||
private FileUtils fileUtils; | |||
@@ -0,0 +1,137 @@ | |||
/* | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2002 The Apache Software Foundation. All rights | |||
* reserved. | |||
* | |||
* Redistribution and use in source and binary forms, with or without | |||
* modification, are permitted provided that the following conditions | |||
* are met: | |||
* | |||
* 1. Redistributions of source code must retain the above copyright | |||
* notice, this list of conditions and the following disclaimer. | |||
* | |||
* 2. Redistributions in binary form must reproduce the above copyright | |||
* notice, this list of conditions and the following disclaimer in | |||
* the documentation and/or other materials provided with the | |||
* distribution. | |||
* | |||
* 3. The end-user documentation included with the redistribution, if | |||
* any, must include the following acknowlegement: | |||
* "This product includes software developed by the | |||
* Apache Software Foundation (http://www.apache.org/)." | |||
* Alternately, this acknowlegement may appear in the software itself, | |||
* if and wherever such third-party acknowlegements normally appear. | |||
* | |||
* 4. The names "The Jakarta Project", "Ant", and "Apache Software | |||
* Foundation" must not be used to endorse or promote products derived | |||
* from this software without prior written permission. For written | |||
* permission, please contact apache@apache.org. | |||
* | |||
* 5. Products derived from this software may not be called "Apache" | |||
* nor may "Apache" appear in their names without prior written | |||
* permission of the Apache Group. | |||
* | |||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||
* SUCH DAMAGE. | |||
* ==================================================================== | |||
* | |||
* This software consists of voluntary contributions made by many | |||
* individuals on behalf of the Apache Software Foundation. For more | |||
* information on the Apache Software Foundation, please see | |||
* <http://www.apache.org/>. | |||
*/ | |||
package org.apache.tools.ant.input; | |||
import java.io.BufferedReader; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.io.InputStreamReader; | |||
import java.util.Enumeration; | |||
import org.apache.tools.ant.BuildException; | |||
/** | |||
* Prompts on System.out, reads input from System.in | |||
* | |||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
* @version $Revision$ | |||
* @since Ant 1.5 | |||
*/ | |||
public class DefaultInputHandler implements InputHandler { | |||
/** | |||
* Empty no-arg constructor | |||
*/ | |||
public DefaultInputHandler() { | |||
} | |||
/** | |||
* Prompts and requests input. May loop until a valid input has | |||
* been entered. | |||
*/ | |||
public void handleInput(InputRequest request) throws BuildException { | |||
String prompt = getPrompt(request); | |||
BufferedReader in = | |||
new BufferedReader(new InputStreamReader(getInputStream())); | |||
do { | |||
System.out.println(prompt); | |||
try { | |||
String input = in.readLine(); | |||
request.setInput(input); | |||
} catch (IOException e) { | |||
throw new BuildException("Failed to read input from Console.", | |||
e); | |||
} | |||
} while (!request.isInputValid()); | |||
} | |||
/** | |||
* Constructs user prompt from a request. | |||
* | |||
* <p>This implementation adds (choice1,choice2,choice3,...) to the | |||
* prompt for <code>MultipleChoiceInputRequest</code>s.</p> | |||
* | |||
* @param request the request to construct the prompt for. | |||
* Must not be <code>null</code>. | |||
*/ | |||
protected String getPrompt(InputRequest request) { | |||
String prompt = request.getPrompt(); | |||
if (request instanceof MultipleChoiceInputRequest) { | |||
StringBuffer sb = new StringBuffer(prompt); | |||
sb.append("("); | |||
Enumeration enum = | |||
((MultipleChoiceInputRequest) request).getChoices().elements(); | |||
boolean first = true; | |||
while (enum.hasMoreElements()) { | |||
if (!first) { | |||
sb.append(","); | |||
} | |||
sb.append(enum.nextElement()); | |||
first = false; | |||
} | |||
sb.append(")"); | |||
prompt = sb.toString(); | |||
} | |||
return prompt; | |||
} | |||
/** | |||
* Returns the input stream from which the user input should be read. | |||
*/ | |||
protected InputStream getInputStream() { | |||
return System.in; | |||
} | |||
} |
@@ -0,0 +1,77 @@ | |||
/* | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2002 The Apache Software Foundation. All rights | |||
* reserved. | |||
* | |||
* Redistribution and use in source and binary forms, with or without | |||
* modification, are permitted provided that the following conditions | |||
* are met: | |||
* | |||
* 1. Redistributions of source code must retain the above copyright | |||
* notice, this list of conditions and the following disclaimer. | |||
* | |||
* 2. Redistributions in binary form must reproduce the above copyright | |||
* notice, this list of conditions and the following disclaimer in | |||
* the documentation and/or other materials provided with the | |||
* distribution. | |||
* | |||
* 3. The end-user documentation included with the redistribution, if | |||
* any, must include the following acknowlegement: | |||
* "This product includes software developed by the | |||
* Apache Software Foundation (http://www.apache.org/)." | |||
* Alternately, this acknowlegement may appear in the software itself, | |||
* if and wherever such third-party acknowlegements normally appear. | |||
* | |||
* 4. The names "The Jakarta Project", "Ant", and "Apache Software | |||
* Foundation" must not be used to endorse or promote products derived | |||
* from this software without prior written permission. For written | |||
* permission, please contact apache@apache.org. | |||
* | |||
* 5. Products derived from this software may not be called "Apache" | |||
* nor may "Apache" appear in their names without prior written | |||
* permission of the Apache Group. | |||
* | |||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||
* SUCH DAMAGE. | |||
* ==================================================================== | |||
* | |||
* This software consists of voluntary contributions made by many | |||
* individuals on behalf of the Apache Software Foundation. For more | |||
* information on the Apache Software Foundation, please see | |||
* <http://www.apache.org/>. | |||
*/ | |||
package org.apache.tools.ant.input; | |||
/** | |||
* Plugin to Ant to handle requests for user input. | |||
* | |||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
* @version $Revision$ | |||
* @since Ant 1.5 | |||
*/ | |||
public interface InputHandler { | |||
/** | |||
* Handle the request encapsulated in the argument. | |||
* | |||
* <p>Precondition: the request.getPrompt will return a non-null | |||
* value.</p> | |||
* | |||
* <p>Postcondition: request.getInput will return a non-null | |||
* value, request.isInputValid will return true.</p> | |||
*/ | |||
void handleInput(InputRequest request) | |||
throws org.apache.tools.ant.BuildException; | |||
} |
@@ -0,0 +1,107 @@ | |||
/* | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2002 The Apache Software Foundation. All rights | |||
* reserved. | |||
* | |||
* Redistribution and use in source and binary forms, with or without | |||
* modification, are permitted provided that the following conditions | |||
* are met: | |||
* | |||
* 1. Redistributions of source code must retain the above copyright | |||
* notice, this list of conditions and the following disclaimer. | |||
* | |||
* 2. Redistributions in binary form must reproduce the above copyright | |||
* notice, this list of conditions and the following disclaimer in | |||
* the documentation and/or other materials provided with the | |||
* distribution. | |||
* | |||
* 3. The end-user documentation included with the redistribution, if | |||
* any, must include the following acknowlegement: | |||
* "This product includes software developed by the | |||
* Apache Software Foundation (http://www.apache.org/)." | |||
* Alternately, this acknowlegement may appear in the software itself, | |||
* if and wherever such third-party acknowlegements normally appear. | |||
* | |||
* 4. The names "The Jakarta Project", "Ant", and "Apache Software | |||
* Foundation" must not be used to endorse or promote products derived | |||
* from this software without prior written permission. For written | |||
* permission, please contact apache@apache.org. | |||
* | |||
* 5. Products derived from this software may not be called "Apache" | |||
* nor may "Apache" appear in their names without prior written | |||
* permission of the Apache Group. | |||
* | |||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||
* SUCH DAMAGE. | |||
* ==================================================================== | |||
* | |||
* This software consists of voluntary contributions made by many | |||
* individuals on behalf of the Apache Software Foundation. For more | |||
* information on the Apache Software Foundation, please see | |||
* <http://www.apache.org/>. | |||
*/ | |||
package org.apache.tools.ant.input; | |||
/** | |||
* Encapsulates an input request. | |||
* | |||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
* @version $Revision$ | |||
* @since Ant 1.5 | |||
*/ | |||
public class InputRequest { | |||
private String prompt; | |||
private String input; | |||
/** | |||
* @param prompt The prompt to show to the user. Must not be null. | |||
*/ | |||
public InputRequest(String prompt) { | |||
if (prompt == null) { | |||
throw new IllegalArgumentException("prompt must not be null"); | |||
} | |||
this.prompt = prompt; | |||
} | |||
/** | |||
* Retrieves the prompt text. | |||
*/ | |||
public String getPrompt() { | |||
return prompt; | |||
} | |||
/** | |||
* Sets the user provided input. | |||
*/ | |||
public void setInput(String input) { | |||
this.input = input; | |||
} | |||
/** | |||
* Is the user input valid? | |||
*/ | |||
public boolean isInputValid() { | |||
return true; | |||
} | |||
/** | |||
* Retrieves the user input. | |||
*/ | |||
public String getInput() { | |||
return input; | |||
} | |||
} |
@@ -0,0 +1,95 @@ | |||
/* | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2002 The Apache Software Foundation. All rights | |||
* reserved. | |||
* | |||
* Redistribution and use in source and binary forms, with or without | |||
* modification, are permitted provided that the following conditions | |||
* are met: | |||
* | |||
* 1. Redistributions of source code must retain the above copyright | |||
* notice, this list of conditions and the following disclaimer. | |||
* | |||
* 2. Redistributions in binary form must reproduce the above copyright | |||
* notice, this list of conditions and the following disclaimer in | |||
* the documentation and/or other materials provided with the | |||
* distribution. | |||
* | |||
* 3. The end-user documentation included with the redistribution, if | |||
* any, must include the following acknowlegement: | |||
* "This product includes software developed by the | |||
* Apache Software Foundation (http://www.apache.org/)." | |||
* Alternately, this acknowlegement may appear in the software itself, | |||
* if and wherever such third-party acknowlegements normally appear. | |||
* | |||
* 4. The names "The Jakarta Project", "Ant", and "Apache Software | |||
* Foundation" must not be used to endorse or promote products derived | |||
* from this software without prior written permission. For written | |||
* permission, please contact apache@apache.org. | |||
* | |||
* 5. Products derived from this software may not be called "Apache" | |||
* nor may "Apache" appear in their names without prior written | |||
* permission of the Apache Group. | |||
* | |||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||
* SUCH DAMAGE. | |||
* ==================================================================== | |||
* | |||
* This software consists of voluntary contributions made by many | |||
* individuals on behalf of the Apache Software Foundation. For more | |||
* information on the Apache Software Foundation, please see | |||
* <http://www.apache.org/>. | |||
*/ | |||
package org.apache.tools.ant.input; | |||
import java.util.Vector; | |||
/** | |||
* Encapsulates an input request. | |||
* | |||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
* @version $Revision$ | |||
* @since Ant 1.5 | |||
*/ | |||
public class MultipleChoiceInputRequest extends InputRequest { | |||
private Vector choices = new Vector(); | |||
/** | |||
* @param prompt The prompt to show to the user. Must not be null. | |||
* @param choices holds all input values that are allowed. | |||
* Must not be null. | |||
*/ | |||
public MultipleChoiceInputRequest(String prompt, Vector choices) { | |||
super(prompt); | |||
if (choices == null) { | |||
throw new IllegalArgumentException("choices must not be null"); | |||
} | |||
this.choices = choices; | |||
} | |||
/** | |||
* @return The possible values. | |||
*/ | |||
public Vector getChoices() { | |||
return choices; | |||
} | |||
/** | |||
* @return true if the input is one of the allowed values. | |||
*/ | |||
public boolean isInputValid() { | |||
return choices.contains(getInput()); | |||
} | |||
} |
@@ -0,0 +1,128 @@ | |||
/* | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2002 The Apache Software Foundation. All rights | |||
* reserved. | |||
* | |||
* Redistribution and use in source and binary forms, with or without | |||
* modification, are permitted provided that the following conditions | |||
* are met: | |||
* | |||
* 1. Redistributions of source code must retain the above copyright | |||
* notice, this list of conditions and the following disclaimer. | |||
* | |||
* 2. Redistributions in binary form must reproduce the above copyright | |||
* notice, this list of conditions and the following disclaimer in | |||
* the documentation and/or other materials provided with the | |||
* distribution. | |||
* | |||
* 3. The end-user documentation included with the redistribution, if | |||
* any, must include the following acknowlegement: | |||
* "This product includes software developed by the | |||
* Apache Software Foundation (http://www.apache.org/)." | |||
* Alternately, this acknowlegement may appear in the software itself, | |||
* if and wherever such third-party acknowlegements normally appear. | |||
* | |||
* 4. The names "The Jakarta Project", "Ant", and "Apache Software | |||
* Foundation" must not be used to endorse or promote products derived | |||
* from this software without prior written permission. For written | |||
* permission, please contact apache@apache.org. | |||
* | |||
* 5. Products derived from this software may not be called "Apache" | |||
* nor may "Apache" appear in their names without prior written | |||
* permission of the Apache Group. | |||
* | |||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||
* SUCH DAMAGE. | |||
* ==================================================================== | |||
* | |||
* This software consists of voluntary contributions made by many | |||
* individuals on behalf of the Apache Software Foundation. For more | |||
* information on the Apache Software Foundation, please see | |||
* <http://www.apache.org/>. | |||
*/ | |||
package org.apache.tools.ant.input; | |||
import org.apache.tools.ant.BuildException; | |||
import java.io.FileInputStream; | |||
import java.io.IOException; | |||
import java.util.Properties; | |||
/** | |||
* Reads input from a property file, the file name is read from the | |||
* system property ant.input.properties, the prompt is the key for input. | |||
* | |||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
* @version $Revision$ | |||
* @since Ant 1.5 | |||
*/ | |||
public class PropertyFileInputHandler implements InputHandler { | |||
private Properties props = null; | |||
/** | |||
* Name of the system property we expect to hold the file name. | |||
*/ | |||
public static final String FILE_NAME_KEY = "ant.input.properties"; | |||
/** | |||
* Empty no-arg constructor. | |||
*/ | |||
public PropertyFileInputHandler() { | |||
} | |||
/** | |||
* Picks up the input from a property, using the prompt as the | |||
* name of the property. | |||
* | |||
* @exception BuildException if no property of that name can be found. | |||
*/ | |||
public void handleInput(InputRequest request) throws BuildException { | |||
readProps(); | |||
Object o = props.get(request.getPrompt()); | |||
if (o == null) { | |||
throw new BuildException("Unable to find input for " | |||
+ request.getPrompt()); | |||
} | |||
request.setInput(o.toString()); | |||
if (!request.isInputValid()) { | |||
throw new BuildException("Found invalid input " + o | |||
+ " for " + request.getPrompt()); | |||
} | |||
} | |||
/** | |||
* Reads the properties file if it hasn't already been read. | |||
*/ | |||
private synchronized void readProps() throws BuildException { | |||
if (props == null) { | |||
String propsFile = System.getProperty(FILE_NAME_KEY); | |||
if (propsFile == null) { | |||
throw new BuildException("System property " | |||
+ FILE_NAME_KEY | |||
+ " for PropertyFileInputHandler not" | |||
+ " set"); | |||
} | |||
props = new Properties(); | |||
try { | |||
props.load(new FileInputStream(propsFile)); | |||
} catch (IOException e) { | |||
throw new BuildException("Couldn't load " + propsFile, e); | |||
} | |||
} | |||
} | |||
} |
@@ -54,22 +54,22 @@ | |||
package org.apache.tools.ant.taskdefs; | |||
import java.io.BufferedReader; | |||
import java.io.InputStreamReader; | |||
import java.io.IOException; | |||
import java.util.StringTokenizer; | |||
import java.util.Vector; | |||
import org.apache.tools.ant.Task; | |||
import org.apache.tools.ant.BuildException; | |||
import org.apache.tools.ant.Project; | |||
import org.apache.tools.ant.Task; | |||
import org.apache.tools.ant.input.InputRequest; | |||
import org.apache.tools.ant.input.MultipleChoiceInputRequest; | |||
import org.apache.tools.ant.util.StringUtils; | |||
/** | |||
* Ant task to read input line from console. | |||
* | |||
* @author <a href="mailto:usch@usch.net">Ulrich Schmidt</a> | |||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
* | |||
* @since Ant 1.5 | |||
* | |||
* @ant.task category="control" | |||
*/ | |||
@@ -77,7 +77,6 @@ public class Input extends Task { | |||
private String validargs = null; | |||
private String message = ""; | |||
private String addproperty = null; | |||
private String input = null; | |||
/** | |||
* Defines valid input parameters as comma separated String. If set, input | |||
@@ -111,11 +110,10 @@ public class Input extends Task { | |||
} | |||
/** | |||
* Sets surrogate input to allow automated testing. | |||
* @param input The surrogate input used for testing. | |||
* Set a multiline message. | |||
*/ | |||
public void setTestinput (String testinput) { | |||
this.input = testinput; | |||
public void addText(String msg) { | |||
message += getProject().replaceProperties(msg); | |||
} | |||
/** | |||
@@ -129,53 +127,19 @@ public class Input extends Task { | |||
* @exception BuildException | |||
*/ | |||
public void execute () throws BuildException { | |||
Vector accept = null; | |||
InputRequest request = null; | |||
if (validargs != null) { | |||
accept = new Vector(); | |||
StringTokenizer stok = new StringTokenizer(validargs, ",", false); | |||
while (stok.hasMoreTokens()) { | |||
accept.addElement(stok.nextToken()); | |||
} | |||
} | |||
log(message, Project.MSG_WARN); | |||
if (input == null) { | |||
try { | |||
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); | |||
input = in.readLine(); | |||
if (accept != null) { | |||
while (!accept.contains(input)) { | |||
log(message, Project.MSG_WARN); | |||
input = in.readLine(); | |||
} | |||
} | |||
} catch (IOException e) { | |||
throw new BuildException("Failed to read input from Console.", e); | |||
} | |||
Vector accept = StringUtils.split(validargs, ','); | |||
request = new MultipleChoiceInputRequest(message, accept); | |||
} else { | |||
// not quite the original intention of this task but for the sake | |||
// of testing ;-) | |||
if (accept != null && (!accept.contains(input))) { | |||
throw new BuildException("Invalid input please reenter."); | |||
} | |||
request = new InputRequest(message); | |||
} | |||
// adopted from org.apache.tools.ant.taskdefs.Property | |||
getProject().getInputHandler().handleInput(request); | |||
if (addproperty != null) { | |||
if (project.getProperty(addproperty) == null) { | |||
project.setProperty(addproperty, input); | |||
} else { | |||
log("Override ignored for " + addproperty, Project.MSG_VERBOSE); | |||
} | |||
project.setNewProperty(addproperty, request.getInput()); | |||
} | |||
} | |||
// copied n' pasted from org.apache.tools.ant.taskdefs.Exit | |||
/** | |||
* Set a multiline message. | |||
*/ | |||
public void addText(String msg) { | |||
message += project.replaceProperties(msg); | |||
} | |||
} | |||
@@ -1,7 +1,7 @@ | |||
/* | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2001 The Apache Software Foundation. All rights | |||
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights | |||
* reserved. | |||
* | |||
* Redistribution and use in source and binary forms, with or without | |||
@@ -53,10 +53,13 @@ | |||
*/ | |||
package org.apache.tools.ant.taskdefs; | |||
import org.apache.tools.ant.BuildFileTest; | |||
import org.apache.tools.ant.input.PropertyFileInputHandler; | |||
/** | |||
* @author Ulrich Schmidt <usch@usch.net> | |||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
*/ | |||
public class InputTest extends BuildFileTest { | |||
@@ -65,37 +68,34 @@ public class InputTest extends BuildFileTest { | |||
} | |||
public void setUp() { | |||
System.getProperties() | |||
.put(PropertyFileInputHandler.FILE_NAME_KEY, | |||
"src/etc/testcases/taskdefs/input.properties"); | |||
configureProject("src/etc/testcases/taskdefs/input.xml"); | |||
getProject().setInputHandler(new PropertyFileInputHandler()); | |||
} | |||
public void test1() { | |||
expectLog("test1", "Press Return key to continue..."); | |||
executeTarget("test1"); | |||
} | |||
public void test2() { | |||
expectLog("test2", "Press Return key to continue..."); | |||
executeTarget("test1"); | |||
} | |||
public void test3() { | |||
String log = "All data is going to be deleted from DB continue (y/n)?"; | |||
String message = "Invalid input please reenter."; | |||
try { | |||
executeTarget("test3"); | |||
} catch (org.apache.tools.ant.BuildException e) { | |||
String realLog = getLog(); | |||
assertEquals(log, realLog); | |||
assertEquals(message, e.getMessage()); | |||
} | |||
expectSpecificBuildException("test3", "invalid input", | |||
"Found invalid input test for All data is" | |||
+ " going to be deleted from DB" | |||
+ " continue?"); | |||
} | |||
public void test5() { | |||
expectLog("test5", | |||
"All data is going to be deleted from DB continue (y/n)?"); | |||
executeTarget("test5"); | |||
} | |||
public void test6() { | |||
expectLog("test6", | |||
"Please enter db-username:"); | |||
executeTarget("test6"); | |||
assertEquals("scott", project.getProperty("db.user")); | |||
} | |||