ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ivan Ivanov <>
Subject Re: AW: Creating custom InputHandler
Date Wed, 14 Jul 2004 07:09:14 GMT
Hello Jan,
I implemnted carefully your ideas and here are the
results which I received and some more questions. I am
using ant1.6.2beta1 sources and the new input handler
which I created is called (shown
below). You recommended me to create a task
SetProjectRefrerenceOnInputHanlder, which will call
setProject(Project) method of
PropertyExpandInputHandler. It turned that this task
is not needed, because Ant "magically" sets the
project reference of the input handler. More
this is done in by
setProjectReference() method:

public final void setProjectReference(final Object
obj) {
    if (obj instanceof ProjectComponent) {
        ((ProjectComponent) obj).setProject(this);
    try {
        Method method =
                "setProject", new Class[]
        if (method != null) {
            method.invoke(obj, new Object[] {this});
    } catch (Throwable e) {
        // ignore this if the object does not have
        // a set project method or the method
        // is private/protected.
called on the object paramameters of the build file.
This method shows that it is not necessary to make
PropertyExpandInputHandler extend ProjectComponent,
because it will call its setProject(Project)
via reflection. However, I made it ProjectComponent
for clarity.

Here is the actual code of PropertyExpandInputHandler
and the question it arises.


import java.util.Enumeration;

public class PropertyExpandInputHandler extends
ProjectComponent implements InputHandler {

     * The project this input handler is used in.
     * This reference is needed when we resolve the
    private Project project;

     * Empty no-arg constructor
    public PropertyExpandInputHandler() {

     * Prompts and requests input.  When the input is
     * the properties in it are expanded to their
values set in
     * the current project.
     * May loop until a valid input has been entered.
     * @param request the request to handle
     * @throws BuildException if not possible to read
from console
    public void handleInput(InputRequest request)
throws BuildException {
        String prompt = getPrompt(request);
        DataInputStream in = null;
        try {
            in =
                new DataInputStream(new
            do {
                try {
                    String input = in.readLine();
                    input =
                } catch (IOException e) {
                    throw new BuildException("Failed
to read input from"
                                             + "
Console.", e);
            } while (!request.isInputValid());
        } finally {
            if (in != null) {
                try {
                } catch (IOException e) {
                    throw new BuildException("Failed
to close input.", e);

     * Constructs user prompt from a request.
     * <p>This implementation adds
(choice1,choice2,choice3,...) to the
     * prompt for
     * @param request the request to construct the
prompt for.
     *                Must not be <code>null</code>.
     * @return the prompt to ask the user
    protected String getPrompt(InputRequest request) {
        String prompt = request.getPrompt();
        if (request instanceof
MultipleChoiceInputRequest) {
            StringBuffer sb = new
            Enumeration e =
            boolean first = true;
            while (e.hasMoreElements()) {
                if (!first) {
                first = false;
            prompt = sb.toString();
        return prompt;

     * Returns the input stream from which the user
input should be read.
     * @return the input stream from which the user
input should be read.
    protected InputStream getInputStream() {

     * Sets the project to which this input handler
belongs to.
     * param project the project in which this input
handler is used.
    public void setProject(Project project) {
        this.project = project;

    public Project getProject() {
        return project;

The issue here is that PropertyExpandInputHandler
simply duplicates DefaultInputHandler with the single
difference in the line 
input = project.replaceProperties(input);
I thought that PropertyExpandInputHandler can extend
DefaultInputHandler but then it cannot extend
ProjectComponent. Do you think, I will lose
something if I extend DefaultInputHandler and let ant
call setProject via refelection? I would ask you also
to point me any weaknesses of the above code since it
is my first extesion of Ant :)).

Thank you very much for your help.


--- wrote:
> Indeed, seems to be a problem.
> The initialization procedure for InputHandlers
> doesnt store a project
> reference (and 
> couldnt do it, because the IH-Interface doesnt
> requires a
> setProject(Project) method).
> So only a workaround comes into my mind:
> - your InputHandler has to provide the setProject
> method
> - set the reference for yourself before using the
> handler by using a Task:
>   (code not tested - even compiled):
>   public class SetProjectReferenceOnInputHandler
> extends Task {
>       public void execute() {
>           InputHandler ih =
> getProject().getInputHandler();
>           if (ih instanceof MyInputHandler) {
> ((MyInputHandler)ih).setProject(getProject());
>           }
>       }
>   }
> Maybe we should check if the InputHandler is a
> ProjectComponent and set the
> project
> reference then ... 
> Jan

Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage! 

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message