----- Original Message -----
Sent: Tuesday, July 24, 2001 7:40
PM
Subject: [PATCH]
PropertyPrompt.java
This task prompts build user for property values, allowing interactive
builds. It is somewhat tangential to Ant's primary purpose but useful enough
for inclusion as an optional task.
----------------------------------------
Example build.xml file demonstrating use:
<?xml version="1.0"?>
<project name="PropertyPromptExample"
default="main" basedir=".">
<property name="promptTimeout"
value="0"/>
<taskdef name="propertyprompt"
classname="org.apache.jakarta.tools.ant.taskdefs.optional.PropertyPrompt"/>
<target
name="main">
<property name="propA"
value="oldValA"/>
<property name="propA"
value="oldValA1"/>
<echo>value of propA:
${propA}</echo>
<ech o>value of propB:
${propB}</echo>
<propertyprompt propertyname="propA"
promptcharacter=":">Enter value for
propA</propertyprompt>
<propertyprompt propertyname="propB"
defaultvalue="defvalB">What is the value for
propB</propertyprompt>
<echo>value of propA:
${propA}</echo>
<echo>value of propB:
${propB}</echo>
</target>
</project>
--------------------------------------------------
package org.apache.tools.ant.taskdefs.optional;
/**
* Task that prompts user for property values to allow
interactive builds.
* Admittedly, this task definitely falls way
outside the bounds of
* Ant's core functionality but is useful enough
to warrant
* inclusion amongst the optional tasks.
*
@author: <a href="" PROTECTED]>Anthony
J. Young-Garner</a>
* Set project property "promptTimeout" to
control behavior.
;* timeout = -1 --> Cancel prompting. Use
default property values.
* timeout = 0 --> Wait indefinitely
for user response (default).
* timeout = x --> Wait x
seconds for user reponse before using
default
*
property values (for x > 0). IMPLEMENTATION POSTPONED
UNTIL
* &nbs
p; JDK 1.4 provides non-blocking I/O.
*/
import java.io.InputStreamReader;
import
java.io.BufferedReader;
import java.io.IOException;
import
java.util.Hashtable;
import
org.apache.tools.ant.taskdefs.Property;
public class
PropertyPrompt extends org.apache.tools.ant.Task {
private java.lang.String propertyname;
private
java.lang.String defaultvalue;
private java.lang.String
proposedValue;
private java.lang.String prompttext;
private
java.lang.String promptcharacter;
private int
timeout;
/**
* PropertyPrompt default
constructor.
*/
public PropertyPrompt()
{
super();
}
/**
* Sets the prompt text that will be
presented to the user.
* @param prompt
java.lang.String
*/
public void addText(String prompt)
{
setPrompttext(prompt);
}
/**
* Run the
PropertyPrompt task.
* @exception org.apache.tools.ant.BuildException
The exception description.
*/
public void execute() throws
org.apache.tools.ant.BuildException {
if (timeout > -1)
{
log("Prompting user for " + propertyname + ". Default value
is " + defaultvalue + ".");
StringBuffer prompt = new
StringBuffer();
pro
mpt.append("\n");
prompt.append(prompttext);
prompt.append("
[");
prompt.append(defaultvalue);
prompt.append("]
");
prompt.append(promptcharacter);
prompt.append("
");
System.out.print(prompt.toString());
/** future version should have hooks for validation of user
input.*/
BufferedReader reader = new BufferedReader(new
InputStreamReader (System.in));
try
{
/**
* when we get to JDK 1.4
(yeah, right), this should be non-blocking so that
*
user-input can be time limited. (Threads could be used as a
work-around
* but I'm not comfortable introducing the
complexity of threads for su ch a
* small
feature).
*/
if (timeout < 0)
{
}
proposedValue =
reader.readLine();
} catch (IOException e)
{
log("Prompt failed. Using
default.");
proposedValue =
defaultvalue;
}
if (!proposedValue.equals(""))
{
/**
* According to the mailing
list, properties are API mutable
* (as opposed to
user-properties and the use of multiple
*
<property> tags to 'mutate' property values).
*/
project.setProperty(propertyname,
proposedValue);
}
}
}
/**
*
Returns defaultValue specified
* in this task for the
Property
* being set.
* @return
java.lang.String
*/
public java.lang.String getDefaultvalue() {
return defaultvalue;
}
/**
* Returns the terminating
character used to
* punctuate the prompt text.
*/
public
java.lang.String getPromptcharacter() {
return
promptcharacter;
}
/**
* Returns text of the prompt.
*
@return java.lang.String
*/
public java.lang.String
getPrompttext() {
return prompttext;
}
/**
* Returns
name of the Ant Project Property
* being set by this task.
* @return java.lang.String
*/
public java.lang.String
getPropertyname() {
return propertyname;
}
/**
*
Initializes this task.
*/
public void init()
{
super.init();
String timeoutProperty =
project.getProperty("promptTimeout");
if (timeoutProperty == null) {
timeout = 0;
}
else {
try{
timeout =
Integer.parseInt(timeoutProperty);
}
catch(NumberFormatException e) {
log("Invalid
promptTimeout value: " + timeoutProperty + ". Using default (wait
indefinitely).");
timeout =
0;
}
}
promptcharacter = "?";
}
/**
* Sets defaultValue for
the Property
* being set by this task.
* @param
newDefaultvalue java.lang.String
*/
public void
setDefaultvalue(java.lang.String newDefaultvalue) {
defaultvalue =
newDefaultvalue;
}
/**
* Sets the terminating character used to
* punctuate the prompt text (default is "?").
* @param
newPromptcharacter java.lang.String
*/
public void setPromptcharac
ter(java.lang.String newPromptcharacter) {
promptcharacter =
newPromptcharacter;
}
/**
* Sets text of the prompt.
*
@param newPrompttext java.lang.String
*/
public void
setPrompttext(java.lang.String newPrompttext) {
prompttext =
newPrompttext;
}
/**
* Specifies the Ant Project
Property
* being set by this task.
* @param newPropertyname
java.lang.String
*/
public void setPropertyname(java.lang.String
newProp ertyname) {
propertyname = newPropertyname;
}
}
------------------------------
Do You Yahoo!?
Make international calls for as low as $.04/minute
with Yahoo! Messenger
http://phonecard.yahoo.com/