Dear Colleagues,
Consider the following ant xml file:
<!-- inputpfih.xml -->
<project name="inputpfih" basedir="." default="input">
<description>Test Script for
PropertyFileInputHandler</description>
<target name="input">
<input message="Enter Your Name"
addproperty="pfh.name"/>
<echo>pfh.name is ${pfh.name}</echo>
</target>
</project>
If used with DefaultInputHanlder, it works like a
charm. Now let's try to start it with
PropertyFileInputHandler and make it read the input
from a properties file (residing in the directory of
inputpfih.xml). According to Ant docs: The prompt
encapsulated in a request will be used as the key when
looking up the input inside the properties file. If no
input can be found, the input is considered invalid
and an exception will be thrown.
So we create the following properties file
#inputp.properties
Enter Your Name=Rambius Parkisanius
and start the script in this way
export
ANT_OPTS=-Dant.input.properties=/home/rambius/drafts/ih/inputp.properties
ant -f inputpfih.xml -inputhandler
org.apache.tools.ant.input.PropertyFileInputHandler
The build fails like this:
Buildfile: inputpfih.xml
input:
BUILD FAILED
/home/rambius/drafts/ih/inputpfih.xml:6: Unable to
find input for 'Enter Your Name'
The cause of the failure is that the key 'Enter Your
Name' used in the properties file contains spaces and
thus cannot be picked up. While in this simple case we
can remove the spaces, I am not sure we should use the
request's propmt as a key. First, spaces sometimes
must be used; second, prompts are subject of frequent
change - on a customer demand, on a QA demand, etc and
when changed we should change the properties file.
What remains "hidden" and is not changed is
addproperty attribute, so I suggest that we use it as
a key in the properties file.
In fact, it does not requires so much coding. Here are
the steps:
1) Add a String field in InputRequest, called property
(for example) with a getter and a setter;
2) Call
request.setProperty(addproperty);
in Input.execute() method before
getInputHandler().handleInput(request) is called.
3) Call
Object o = props.get(request.getProperty());
instead of
Object o = props.get(request.getPrompt());
in PropertyFileInputHandler.handleInput(InputRequest).
(Two more lines should be changed but this is one is
most important)
After building ant with the above change, we create
the following properties file:
#inputpfih.properties
pfh.name=Rambius Parkisanius
and start the ant script in this way
export
ANT_OPTS=-Dant.input.properties=/home/rambius/drafts/ih/inputpfih.properties
ant -f inputpfih.xml -inputhandler
org.apache.tools.ant.input.PropertyFileInputHandler
The result is as expected:
Buildfile: inputpfih.xml
input:
[echo] pfh.name is Rambius Parkisanius
BUILD SUCCESSFUL
I am sending also the patches generated against the
CVS HEAD and if Ant developers find them useful, I
would be more than glad to contribute them and if
needed I can also write the unit tests.
I encountered this behaviour of
PropertyFileInputHandler while writing a custom task,
from which I call Input task several times. The
prompts in it are dynamically created Strings with new
lines in them and I tried to use
PropertyFileInputHandler to automate the testing of
this task. Even if I could read keys with spaces from
properties file, it would be hard to manage multiline
keys.
Regards and let me know your opinion
Ivan
__________________________________
Do you Yahoo!?
Y! Messenger - Communicate in real time. Download now.
http://messenger.yahoo.com? input.diff
Index: docs/manual/inputhandler.html
===================================================================
RCS file: /home/cvspublic/ant/docs/manual/inputhandler.html,v
retrieving revision 1.2
diff -c -r1.2 inputhandler.html
*** docs/manual/inputhandler.html 9 Feb 2004 21:50:05 -0000 1.2
--- docs/manual/inputhandler.html 27 Oct 2004 15:33:06 -0000
***************
*** 61,70 ****
the properties file must be specified in the Java system property
<code>ant.input.properties</code>.</p>
! <p>The prompt encapsulated in a <code>request</code> will be used as
! the key when looking up the input inside the properties file. If no
! input can be found, the input is considered invalid and an exception
! will be thrown.</p>
<p><strong>Note</strong> that <code>ant.input.properties</code> must
be a Java system property, not an Ant property. I.e. you cannot
--- 61,70 ----
the properties file must be specified in the Java system property
<code>ant.input.properties</code>.</p>
! <p>The contents of <code>addproperty</code> attribute of the corresponding
! <code><input></code> will be used as the key when looking up the
! input inside the properties file. If no input can be found, the input
! is considered invalid and an exception will be thrown.</p>
<p><strong>Note</strong> that <code>ant.input.properties</code> must
be a Java system property, not an Ant property. I.e. you cannot
Index: src/main/org/apache/tools/ant/input/InputRequest.java
===================================================================
RCS file: /home/cvspublic/ant/src/main/org/apache/tools/ant/input/InputRequest.java,v
retrieving revision 1.8
diff -c -r1.8 InputRequest.java
*** src/main/org/apache/tools/ant/input/InputRequest.java 9 Mar 2004 16:48:03 -0000 1.8
--- src/main/org/apache/tools/ant/input/InputRequest.java 27 Oct 2004 15:33:07 -0000
***************
*** 26,31 ****
--- 26,32 ----
public class InputRequest {
private String prompt;
private String input;
+ private String property;
/**
* @param prompt The prompt to show to the user. Must not be null.
***************
*** 64,69 ****
--- 65,78 ----
*/
public String getInput() {
return input;
+ }
+
+ public void setProperty(String property) {
+ this.property = property;
+ }
+
+ public String getProperty() {
+ return property;
}
}
Index: src/main/org/apache/tools/ant/input/PropertyFileInputHandler.java
===================================================================
RCS file: /home/cvspublic/ant/src/main/org/apache/tools/ant/input/PropertyFileInputHandler.java,v
retrieving revision 1.10
diff -c -r1.10 PropertyFileInputHandler.java
*** src/main/org/apache/tools/ant/input/PropertyFileInputHandler.java 9 Mar 2004 16:48:03 -0000 1.10
--- src/main/org/apache/tools/ant/input/PropertyFileInputHandler.java 27 Oct 2004 15:33:07 -0000
***************
*** 52,66 ****
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() + "\'");
}
}
--- 52,66 ----
public void handleInput(InputRequest request) throws BuildException {
readProps();
! Object o = props.get(request.getProperty());
if (o == null) {
throw new BuildException("Unable to find input for \'"
! + request.getProperty() + "\'");
}
request.setInput(o.toString());
if (!request.isInputValid()) {
throw new BuildException("Found invalid input " + o
! + " for \'" + request.getProperty() + "\'");
}
}
Index: src/main/org/apache/tools/ant/taskdefs/Input.java
===================================================================
RCS file: /home/cvspublic/ant/src/main/org/apache/tools/ant/taskdefs/Input.java,v
retrieving revision 1.28
diff -c -r1.28 Input.java
*** src/main/org/apache/tools/ant/taskdefs/Input.java 9 Mar 2004 16:48:05 -0000 1.28
--- src/main/org/apache/tools/ant/taskdefs/Input.java 27 Oct 2004 15:33:07 -0000
***************
*** 112,118 ****
} else {
request = new InputRequest(message);
}
!
getProject().getInputHandler().handleInput(request);
String value = request.getInput();
--- 112,119 ----
} else {
request = new InputRequest(message);
}
! request.setProperty(addproperty);
!
getProject().getInputHandler().handleInput(request);
String value = request.getInput();
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]