Browse Source

<tstamp>'s prefix attribute didn't apply to nested <format>s.

The code tries to use the prefix but fails because setPrefix() gets
called after createFormat().

PR: 12829
Submitted by:   gjfdh at yahoo.com


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@273354 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 22 years ago
parent
commit
f1fd5e5e8a
3 changed files with 31 additions and 11 deletions
  1. +2
    -0
      WHATSNEW
  2. +16
    -11
      src/main/org/apache/tools/ant/taskdefs/Tstamp.java
  3. +13
    -0
      src/testcases/org/apache/tools/ant/taskdefs/TStampTest.java

+ 2
- 0
WHATSNEW View File

@@ -32,6 +32,8 @@ Fixed bugs:
* <replaceregexp> could append a newline character at the end of the
file.

* <tstamp>'s prefix attribute failed to apply to nested <format> elements.

Other changes:
--------------



+ 16
- 11
src/main/org/apache/tools/ant/taskdefs/Tstamp.java View File

@@ -115,14 +115,14 @@ public class Tstamp extends Task {
}

SimpleDateFormat dstamp = new SimpleDateFormat ("yyyyMMdd");
getProject().setNewProperty(prefix + "DSTAMP", dstamp.format(d));
setProperty("DSTAMP", dstamp.format(d));

SimpleDateFormat tstamp = new SimpleDateFormat ("HHmm");
getProject().setNewProperty(prefix + "TSTAMP", tstamp.format(d));
setProperty("TSTAMP", tstamp.format(d));

SimpleDateFormat today
= new SimpleDateFormat ("MMMM d yyyy", Locale.US);
getProject().setNewProperty(prefix + "TODAY", today.format(d));
setProperty("TODAY", today.format(d));

} catch (Exception e) {
throw new BuildException(e);
@@ -134,11 +134,19 @@ public class Tstamp extends Task {
* @return a ready to fill-in format
*/
public CustomFormat createFormat() {
CustomFormat cts = new CustomFormat(prefix);
CustomFormat cts = new CustomFormat();
customFormats.addElement(cts);
return cts;
}

/**
* helper that encapsulates prefix logic and property setting
* policy (i.e. we use setNewProperty instead of setProperty).
*/
private void setProperty(String name, String value) {
getProject().setNewProperty(prefix + name, value);
}

/**
* This nested element that allows a property to be set
* to the current date and time in a given format.
@@ -157,14 +165,11 @@ public class Tstamp extends Task {
private String variant;
private int offset = 0;
private int field = Calendar.DATE;
private String prefix = "";

/**
* Create a format with the current prefix
* @param prefix
* Create a format
*/
public CustomFormat(String prefix) {
this.prefix = prefix;
public CustomFormat() {
}

/**
@@ -172,7 +177,7 @@ public class Tstamp extends Task {
* @param propertyName
*/
public void setProperty(String propertyName) {
this.propertyName = prefix + propertyName;
this.propertyName = propertyName;
}

/**
@@ -306,7 +311,7 @@ public class Tstamp extends Task {
if (timeZone != null){
sdf.setTimeZone(timeZone);
}
project.setNewProperty(propertyName, sdf.format(date));
Tstamp.this.setProperty(propertyName, sdf.format(date));
}
}



+ 13
- 0
src/testcases/org/apache/tools/ant/taskdefs/TStampTest.java View File

@@ -121,6 +121,7 @@ public class TStampTest extends TestCase {
assertEquals(expected, today);

}

/**
* verifies that custom props have priority over the
* originals
@@ -133,4 +134,16 @@ public class TStampTest extends TestCase {
assertNotNull(prop);
}

public void testFormatPrefix() throws Exception {
Tstamp.CustomFormat format = tstamp.createFormat();
format.setProperty("format");
format.setPattern("HH:mm:ss z");
format.setTimezone("GMT");

tstamp.setPrefix("prefix");
tstamp.execute();
String prop= project.getProperty("prefix.format");
assertNotNull(prop);
}

}

Loading…
Cancel
Save