From 9f471a747bcb53d63ae806b4b5937dc47641c67c Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Mon, 25 Oct 2010 16:06:12 +0000 Subject: [PATCH] preserve linefeed-style in propertyfile task. PR 50049 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@1027179 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 4 ++ docs/manual/Tasks/propertyfile.html | 7 ++- .../ant/util/LayoutPreservingProperties.java | 48 +++++++++++++++++-- .../optional/propertyfilelayout-test.xml | 45 +++++++++++++++++ 4 files changed, 100 insertions(+), 4 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index e8fc11726..882225dce 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -183,6 +183,10 @@ Fixed bugs: wrong. Bugzilla Report 45227. + * didn't preserve the original linefeed style when + updating a file. + Bugzilla Report 50049. + Other changes: -------------- diff --git a/docs/manual/Tasks/propertyfile.html b/docs/manual/Tasks/propertyfile.html index 27eef7b62..fde99b887 100644 --- a/docs/manual/Tasks/propertyfile.html +++ b/docs/manual/Tasks/propertyfile.html @@ -32,9 +32,14 @@ very useful when wanting to make unattended modifications to configuration files for application servers and applications. Currently, the task maintains a working property file with the ability to add properties or make changes to existing -ones. Since Ant 1.8.0 comments and layout of the original properties +ones. Since Ant 1.8.0 comments and layout of the original properties file are preserved.

+

Since Ant 1.8.2 the linefeed-style of the original file + will be preserved as well, as long as style used to be consistent. + In general, linefeeds of the updated file will be the same as the + first linefeed found when reading it.

+

PropertyFile Task

Parameters

diff --git a/src/main/org/apache/tools/ant/util/LayoutPreservingProperties.java b/src/main/org/apache/tools/ant/util/LayoutPreservingProperties.java index 88ce7f532..ed7385a18 100644 --- a/src/main/org/apache/tools/ant/util/LayoutPreservingProperties.java +++ b/src/main/org/apache/tools/ant/util/LayoutPreservingProperties.java @@ -27,6 +27,7 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintStream; +import java.io.PushbackReader; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -78,7 +79,7 @@ import java.util.Properties; * although the key-value pair beta=two is removed.

*/ public class LayoutPreservingProperties extends Properties { - private static final String LS = System.getProperty("line.separator"); + private String LS = StringUtils.LINE_SEP; /** * Logical lines have escaping and line continuation taken care @@ -310,14 +311,15 @@ public class LayoutPreservingProperties extends Properties { */ private String readLines(InputStream is) throws IOException { InputStreamReader isr = new InputStreamReader(is, ResourceUtils.ISO_8859_1); - BufferedReader br = new BufferedReader(isr); + PushbackReader pbr = new PushbackReader(isr, 1); if (logicalLines.size() > 0) { // we add a blank line for spacing logicalLines.add(new Blank()); } - String s = br.readLine(); + String s = readFirstLine(pbr); + BufferedReader br = new BufferedReader(pbr); boolean continuation = false; boolean comment = false; @@ -366,6 +368,46 @@ public class LayoutPreservingProperties extends Properties { return fileBuffer.toString(); } + /** + * Reads the first line and determines the EOL-style of the file + * (relies on the style to be consistent, of course). + * + *

Sets LS as a side-effect.

+ * + * @return the first line without any line separator, leaves the + * reader positioned after the first line separator + * + * @since Ant 1.8.2 + */ + private String readFirstLine(PushbackReader r) throws IOException { + StringBuffer sb = new StringBuffer(80); + int ch = r.read(); + boolean hasCR = false; + // when reaching EOF before the first EOL, assume native line + // feeds + LS = StringUtils.LINE_SEP; + + while (ch >= 0) { + if (hasCR && ch != '\n') { + // line feed is sole CR + r.unread(ch); + break; + } + + if (ch == '\r') { + LS = "\r"; + hasCR = true; + } else if (ch == '\n') { + LS = hasCR ? "\r\n" : "\n"; + break; + } else { + sb.append((char) ch); + } + ch = r.read(); + } + return sb.toString(); + } + /** * Returns true if the line represented by * s is to be continued on the next line of the file, diff --git a/src/tests/antunit/taskdefs/optional/propertyfilelayout-test.xml b/src/tests/antunit/taskdefs/optional/propertyfilelayout-test.xml index c22ffd1c3..f1b367f97 100644 --- a/src/tests/antunit/taskdefs/optional/propertyfilelayout-test.xml +++ b/src/tests/antunit/taskdefs/optional/propertyfilelayout-test.xml @@ -241,4 +241,49 @@ x=1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +