Hi!
While working on my own ant task, I discovered that
ProjectHelper.replaceProperties removes '$' while it is
substituting for ${...}. This broke my task so I changed the method to the
below. ( my code replaces ${...}, but leaves $ followed by not-{ alone)
Is this behavior intentional or is it a bug?
If this is a bug I would appreciate some fix, either mine or another, being
checked in. If this is not a bug, can someone suggest a way of escaping
the $ character?
Also, what is the appropriate procedure for contributing a new optional
task?
thanks
david jencks
/** Replace ${NAME} with the property value
*/
public static String replaceProperties(Project project, String value,
Hashtable keys )
throws BuildException
{
// XXX use Map instead of proj, it's too heavy
// XXX need to replace this code with something better.
//How's this?
//previous version did not correctly handle $ in value.
StringBuffer sb=new StringBuffer();
int copiedTo=0;
// assert value!=nil
int dollarPos;
while( (dollarPos=value.indexOf( "$", copiedTo )) >= 0 ) {
if (value.charAt( dollarPos + 1 ) == '{') {
//${ found, look for terminating }
int closeBracket = value.indexOf( '}', dollarPos + 1);
if (closeBracket < 0) {
throw new BuildException("Syntax error in prop: " +
value + "--close } missing" );
}
String name=value.substring( dollarPos+2, closeBracket
);
if (keys.containsKey(name)) {
//name found, replace with value.
sb.append(value.substring(copiedTo, dollarPos));
sb.append(keys.get(name));
}
else {
//name not found, copy literally
sb.append(value.substring(copiedTo, closeBracket +
1));
}
copiedTo = closeBracket + 1;
}
else {
//$<not {> found, treat like regular character
sb.append(value.substring(copiedTo, dollarPos + 1));
copiedTo = dollarPos + 1;
}
} //end while
if( copiedTo < value.length() ) sb.append( value.substring(
copiedTo ) );
// System.out.println("After replace: " + sb.toString());
// System.out.println("Before replace: " + value);
return sb.toString();
}