From 550607147fe6a0c89a1b6e6c85f5e69b334d31d7 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Mon, 22 Apr 2002 11:55:21 +0000 Subject: [PATCH] merge input propsal into main tree. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272541 13f79535-47bb-0310-9956-ffa450edef68 --- src/etc/testcases/taskdefs/input.properties | 4 + src/etc/testcases/taskdefs/input.xml | 15 +- src/main/org/apache/tools/ant/Main.java | 53 +++++++ src/main/org/apache/tools/ant/Project.java | 20 +++ .../tools/ant/input/DefaultInputHandler.java | 137 ++++++++++++++++++ .../apache/tools/ant/input/InputHandler.java | 77 ++++++++++ .../apache/tools/ant/input/InputRequest.java | 107 ++++++++++++++ .../ant/input/MultipleChoiceInputRequest.java | 95 ++++++++++++ .../ant/input/PropertyFileInputHandler.java | 128 ++++++++++++++++ .../org/apache/tools/ant/taskdefs/Input.java | 72 +++------ .../apache/tools/ant/taskdefs/InputTest.java | 32 ++-- 11 files changed, 660 insertions(+), 80 deletions(-) create mode 100644 src/etc/testcases/taskdefs/input.properties create mode 100644 src/main/org/apache/tools/ant/input/DefaultInputHandler.java create mode 100644 src/main/org/apache/tools/ant/input/InputHandler.java create mode 100644 src/main/org/apache/tools/ant/input/InputRequest.java create mode 100644 src/main/org/apache/tools/ant/input/MultipleChoiceInputRequest.java create mode 100644 src/main/org/apache/tools/ant/input/PropertyFileInputHandler.java diff --git a/src/etc/testcases/taskdefs/input.properties b/src/etc/testcases/taskdefs/input.properties new file mode 100644 index 000000000..c6981c720 --- /dev/null +++ b/src/etc/testcases/taskdefs/input.properties @@ -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 diff --git a/src/etc/testcases/taskdefs/input.xml b/src/etc/testcases/taskdefs/input.xml index e9d3fcf0f..a2ec247fd 100644 --- a/src/etc/testcases/taskdefs/input.xml +++ b/src/etc/testcases/taskdefs/input.xml @@ -3,32 +3,27 @@ - Press Return key to continue... + Press Return key to continue... - + - - - diff --git a/src/main/org/apache/tools/ant/Main.java b/src/main/org/apache/tools/ant/Main.java index ec03c769c..427bb1690 100644 --- a/src/main/org/apache/tools/ant/Main.java +++ b/src/main/org/apache/tools/ant/Main.java @@ -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 diff --git a/src/main/org/apache/tools/ant/Project.java b/src/main/org/apache/tools/ant/Project.java index 3d74933e0..109dda6b0 100644 --- a/src/main/org/apache/tools/ant/Project.java +++ b/src/main/org/apache/tools/ant/Project.java @@ -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; diff --git a/src/main/org/apache/tools/ant/input/DefaultInputHandler.java b/src/main/org/apache/tools/ant/input/DefaultInputHandler.java new file mode 100644 index 000000000..679e3574f --- /dev/null +++ b/src/main/org/apache/tools/ant/input/DefaultInputHandler.java @@ -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 + * . + */ + +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 Stefan Bodewig + * @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. + * + *

This implementation adds (choice1,choice2,choice3,...) to the + * prompt for MultipleChoiceInputRequests.

+ * + * @param request the request to construct the prompt for. + * Must not be null. + */ + 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; + } + +} diff --git a/src/main/org/apache/tools/ant/input/InputHandler.java b/src/main/org/apache/tools/ant/input/InputHandler.java new file mode 100644 index 000000000..da8a148fd --- /dev/null +++ b/src/main/org/apache/tools/ant/input/InputHandler.java @@ -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 + * . + */ + +package org.apache.tools.ant.input; + +/** + * Plugin to Ant to handle requests for user input. + * + * @author Stefan Bodewig + * @version $Revision$ + * @since Ant 1.5 + */ +public interface InputHandler { + + /** + * Handle the request encapsulated in the argument. + * + *

Precondition: the request.getPrompt will return a non-null + * value.

+ * + *

Postcondition: request.getInput will return a non-null + * value, request.isInputValid will return true.

+ */ + void handleInput(InputRequest request) + throws org.apache.tools.ant.BuildException; +} diff --git a/src/main/org/apache/tools/ant/input/InputRequest.java b/src/main/org/apache/tools/ant/input/InputRequest.java new file mode 100644 index 000000000..3e4c232a1 --- /dev/null +++ b/src/main/org/apache/tools/ant/input/InputRequest.java @@ -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 + * . + */ + +package org.apache.tools.ant.input; + +/** + * Encapsulates an input request. + * + * @author Stefan Bodewig + * @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; + } + +} diff --git a/src/main/org/apache/tools/ant/input/MultipleChoiceInputRequest.java b/src/main/org/apache/tools/ant/input/MultipleChoiceInputRequest.java new file mode 100644 index 000000000..783accdf9 --- /dev/null +++ b/src/main/org/apache/tools/ant/input/MultipleChoiceInputRequest.java @@ -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 + * . + */ + +package org.apache.tools.ant.input; + +import java.util.Vector; + +/** + * Encapsulates an input request. + * + * @author Stefan Bodewig + * @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()); + } +} diff --git a/src/main/org/apache/tools/ant/input/PropertyFileInputHandler.java b/src/main/org/apache/tools/ant/input/PropertyFileInputHandler.java new file mode 100644 index 000000000..19eb5c997 --- /dev/null +++ b/src/main/org/apache/tools/ant/input/PropertyFileInputHandler.java @@ -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 + * . + */ + +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 Stefan Bodewig + * @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); + } + } + } + +} diff --git a/src/main/org/apache/tools/ant/taskdefs/Input.java b/src/main/org/apache/tools/ant/taskdefs/Input.java index d7902ef4d..265c6a884 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Input.java +++ b/src/main/org/apache/tools/ant/taskdefs/Input.java @@ -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 Ulrich Schmidt + * @author Stefan Bodewig + * + * @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); - } } - - - diff --git a/src/testcases/org/apache/tools/ant/taskdefs/InputTest.java b/src/testcases/org/apache/tools/ant/taskdefs/InputTest.java index 54b697904..ab6950889 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/InputTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/InputTest.java @@ -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 + * @author Stefan Bodewig */ 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")); }