diff --git a/WHATSNEW b/WHATSNEW index ee7bb12f3..e29c3c327 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -20,8 +20,8 @@ Fixed bugs: * task didn't report build file location when a remote operation failed Bugzilla Report 48578. - * would add the same comment each time it updated an - existing property file. + * would add the same comment and a date line each time + it updated an existing property file. Bugzilla Report 48558. Other changes: diff --git a/src/main/org/apache/tools/ant/util/DateUtils.java b/src/main/org/apache/tools/ant/util/DateUtils.java index da532c48b..17beaa3e0 100644 --- a/src/main/org/apache/tools/ant/util/DateUtils.java +++ b/src/main/org/apache/tools/ant/util/DateUtils.java @@ -232,6 +232,22 @@ public final class DateUtils { } } + /** + * Parses the string in a format suitable for a SMTP date header. + * + * @param datestr string to be parsed + * + * @return a java.util.Date object as parsed by the format. + * @exception ParseException if the supplied string cannot be parsed by + * this pattern. + * @since Ant 1.8.0 + */ + public static Date parseDateFromHeader(String datestr) throws ParseException { + synchronized (DATE_HEADER_FORMAT_INT) { + return DATE_HEADER_FORMAT_INT.parse(datestr); + } + } + /** * Parse a string as a datetime using the ISO8601_DATETIME format which is * yyyy-MM-dd'T'HH:mm:ss diff --git a/src/main/org/apache/tools/ant/util/LayoutPreservingProperties.java b/src/main/org/apache/tools/ant/util/LayoutPreservingProperties.java index a0650795d..4b4f08854 100644 --- a/src/main/org/apache/tools/ant/util/LayoutPreservingProperties.java +++ b/src/main/org/apache/tools/ant/util/LayoutPreservingProperties.java @@ -253,20 +253,38 @@ public class LayoutPreservingProperties extends Properties { public void store(OutputStream out, String header) throws IOException { OutputStreamWriter osw = new OutputStreamWriter(out, "ISO-8859-1"); + int skipLines = 0; + int totalLines = logicalLines.size(); + if (header != null) { osw.write("#" + header + LS); + if (totalLines > 0 + && logicalLines.get(0) instanceof Comment + && header.equals(logicalLines.get(0).toString().substring(1))) { + skipLines = 1; + } } - osw.write("#" + (new Date()).toString() + LS); + + // we may be updatiung a file written by this class, replace + // the date comment instead of adding a new one and preserving + // the one written last time + if (totalLines > skipLines + && logicalLines.get(skipLines) instanceof Comment) { + try { + DateUtils.parseDateFromHeader(logicalLines + .get(skipLines) + .toString().substring(1)); + skipLines++; + } catch (java.text.ParseException pe) { + // not an existing date comment + } + } + osw.write("#" + DateUtils.getDateForHeader() + LS); boolean writtenSep = false; - boolean maySkipComment = header != null; - for (Iterator i = logicalLines.iterator(); i.hasNext(); - maySkipComment = false) { + for (Iterator i = logicalLines.subList(skipLines, totalLines).iterator(); + i.hasNext(); ) { LogicalLine line = (LogicalLine) i.next(); - if (maySkipComment && line instanceof Comment && - header.equals(line.toString().substring(1))) { - continue; - } if (line instanceof Pair) { if (((Pair)line).isNew()) { if (!writtenSep) { diff --git a/src/tests/antunit/taskdefs/optional/propertyfilelayout-test.xml b/src/tests/antunit/taskdefs/optional/propertyfilelayout-test.xml index cc9b2c441..d13c41acc 100644 --- a/src/tests/antunit/taskdefs/optional/propertyfilelayout-test.xml +++ b/src/tests/antunit/taskdefs/optional/propertyfilelayout-test.xml @@ -190,4 +190,54 @@ x=1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +