diff --git a/WHATSNEW b/WHATSNEW index 1e184fa23..59d193675 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -339,6 +339,8 @@ Other changes: * added the onmissingfiltersfile attribute to filterset. Bugzilla report 19845. +* added the inline handler element to the input task. + Changes from Ant 1.6.4 to Ant 1.6.5 =================================== diff --git a/docs/manual/CoreTasks/input.html b/docs/manual/CoreTasks/input.html index fb234f439..703b75870 100644 --- a/docs/manual/CoreTasks/input.html +++ b/docs/manual/CoreTasks/input.html @@ -78,6 +78,55 @@ file and load in before the input task.

No +

Parameters Specified as Nested Elements

+

Handler

+

Since Ant 1.7, a nested <handler> element can be used to +specify an InputHandler, so that different InputHandlers may be used +among different Input tasks. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
typeone of "default","propertyfile", or "greedy". + One of these
refidReference to an InputHandler + defined elsewhere in the project. +
classnameThe name of an InputHandler subclass.
classpathThe classpath to use with classname.No
classpathrefThe refid of a classpath to use with classname.No
loaderrefThe refid of a classloader to use with classname. + No
+
+The classpath can also be specified by means of one or more nested +<classpath> elements.

+

Examples

  <input/>

Will pause the build run until return key is pressed when using the diff --git a/src/etc/testcases/taskdefs/input.stdin b/src/etc/testcases/taskdefs/input.stdin new file mode 100644 index 000000000..3bd1f0e29 --- /dev/null +++ b/src/etc/testcases/taskdefs/input.stdin @@ -0,0 +1,2 @@ +foo +bar diff --git a/src/etc/testcases/taskdefs/input.xml b/src/etc/testcases/taskdefs/input.xml index a2ec247fd..00c6737a3 100644 --- a/src/etc/testcases/taskdefs/input.xml +++ b/src/etc/testcases/taskdefs/input.xml @@ -28,4 +28,76 @@ /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/Input.java b/src/main/org/apache/tools/ant/taskdefs/Input.java index e0e6c6c22..793f5f89b 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Input.java +++ b/src/main/org/apache/tools/ant/taskdefs/Input.java @@ -1,5 +1,5 @@ /* - * Copyright 2001-2004 The Apache Software Foundation + * Copyright 2001-2005 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,11 +18,20 @@ package org.apache.tools.ant.taskdefs; import java.util.Vector; -import org.apache.tools.ant.BuildException; + import org.apache.tools.ant.Task; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.input.InputHandler; import org.apache.tools.ant.input.InputRequest; +import org.apache.tools.ant.input.GreedyInputHandler; +import org.apache.tools.ant.input.DefaultInputHandler; +import org.apache.tools.ant.input.PropertyFileInputHandler; import org.apache.tools.ant.input.MultipleChoiceInputRequest; +import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.types.EnumeratedAttribute; import org.apache.tools.ant.util.StringUtils; +import org.apache.tools.ant.util.ClasspathUtils; /** * Reads an input line from the console. @@ -32,10 +41,108 @@ import org.apache.tools.ant.util.StringUtils; * @ant.task category="control" */ public class Input extends Task { + + /** + * Represents an InputHandler. + */ + public class Handler extends DefBase { + + private String refid = null; + private HandlerType type = null; + private String classname = null; + + /** + * Specify that the handler is a reference on the project; + * this allows the use of a custom inputhandler. + * @param refid the String refid. + */ + public void setRefid(String refid) { + this.refid = refid; + } + /** + * Get the refid of this Handler. + * @return String refid. + */ + public String getRefid() { + return refid; + } + /** + * Set the InputHandler classname. + * @param classname the String classname. + */ + public void setClassname(String classname) { + this.classname = classname; + } + /** + * Get the classname of the InputHandler. + * @return String classname. + */ + public String getClassname() { + return classname; + } + /** + * Set the handler type. + * @param type a HandlerType. + */ + public void setType(HandlerType type) { + this.type = type; + } + /** + * Get the handler type. + * @return a HandlerType object. + */ + public HandlerType getType() { + return type; + } + private InputHandler getInputHandler() { + if (type != null) { + return type.getInputHandler(); + } + if (refid != null) { + try { + return (InputHandler) (getProject().getReference(refid)); + } catch (ClassCastException e) { + throw new BuildException( + refid + " does not denote an InputHandler", e); + } + } + if (classname != null) { + return (InputHandler) (ClasspathUtils.newInstance(classname, + createLoader(), InputHandler.class)); + } + throw new BuildException( + "Must specify refid, classname or type"); + } + } + + /** + * EnumeratedAttribute representing the built-in input handler types: + * "default", "propertyfile", "greedy". + */ + public static class HandlerType extends EnumeratedAttribute { + private static final String[] VALUES + = {"default", "propertyfile", "greedy"}; + + private static final InputHandler[] HANDLERS + = {new DefaultInputHandler(), + new PropertyFileInputHandler(), + new GreedyInputHandler()}; + + //inherit doc + public String[] getValues() { + return VALUES; + } + private InputHandler getInputHandler() { + return HANDLERS[getIndex()]; + } + } + private String validargs = null; private String message = ""; private String addproperty = null; private String defaultvalue = null; + private Handler handler = null; + private boolean messageAttribute; /** * Defines valid input parameters as comma separated strings. If set, input @@ -66,6 +173,7 @@ public class Input extends Task { */ public void setMessage (String message) { this.message = message; + messageAttribute = true; } /** @@ -84,7 +192,11 @@ public class Input extends Task { * @param msg The message to be displayed. */ public void addText(String msg) { - message += getProject().replaceProperties(msg); + msg = getProject().replaceProperties(msg); + if (messageAttribute && "".equals(msg.trim())) { + return; + } + message += msg; } /** @@ -113,7 +225,11 @@ public class Input extends Task { request = new InputRequest(message); } - getProject().getInputHandler().handleInput(request); + InputHandler h = handler == null + ? getProject().getInputHandler() + : handler.getInputHandler(); + + h.handleInput(request); String value = request.getInput(); if ((value == null || value.trim().length() == 0) @@ -125,4 +241,17 @@ public class Input extends Task { } } + /** + * Create a nested handler element. + * @return a Handler for this Input task. + */ + public Handler createHandler() { + if (handler != null) { + throw new BuildException( + "Cannot define > 1 nested input handler"); + } + handler = new Handler(); + return handler; + } + } diff --git a/src/testcases/org/apache/tools/ant/taskdefs/InputTest.java b/src/testcases/org/apache/tools/ant/taskdefs/InputTest.java index 6829545e0..b64200000 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/InputTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/InputTest.java @@ -17,6 +17,9 @@ package org.apache.tools.ant.taskdefs; +import java.io.InputStream; +import java.io.FileInputStream; + import org.apache.tools.ant.BuildFileTest; import org.apache.tools.ant.input.PropertyFileInputHandler; @@ -62,6 +65,40 @@ public class InputTest extends BuildFileTest { assertEquals("scott", project.getProperty("db.user")); } + public void testPropertyFileInlineHandler() { + executeTarget("testPropertyFileInlineHandler"); + } + + public void testDefaultInlineHandler() { + stdin(); + executeTarget("testDefaultInlineHandler"); + } + + public void testGreedyInlineHandler() { + stdin(); + executeTarget("testGreedyInlineHandler"); + } + + public void testGreedyInlineHandlerClassname() { + stdin(); + executeTarget("testGreedyInlineHandlerClassname"); + } + + public void testGreedyInlineHandlerRefid() { + stdin(); + executeTarget("testGreedyInlineHandlerRefid"); + } + + private void stdin() { + try { + System.setIn(new FileInputStream( + getProject().resolveFile("input.stdin"))); + } catch (Exception e) { + throw e instanceof RuntimeException + ? (RuntimeException) e : new RuntimeException(e.getMessage()); + } + } + private String getKey(String key) { return key; // XXX what is this for? }