Fix properties parsing for comment chars

Update `OriginTrackedPropertiesLoader` to correctly deal with property
values that happen to contain comment characters.

Prior this this commit, values of the following form would not be
parsed correctly:

	foo=bar!
	bar=spam

Closes gh-8647
This commit is contained in:
Dave Syer 2017-03-16 11:57:02 +00:00 committed by Phillip Webb
parent 34de119eba
commit fe8fa2831a
3 changed files with 36 additions and 9 deletions

View File

@ -71,6 +71,7 @@ class OriginTrackedPropertiesLoader {
StringBuilder buffer = new StringBuilder();
while (reader.read()) {
String key = loadKey(buffer, reader).trim();
System.out.println(key);
if (expandLists && key.endsWith("[]")) {
key = key.substring(0, key.length() - 2);
int index = 0;
@ -160,11 +161,19 @@ class OriginTrackedPropertiesLoader {
}
public boolean read() throws IOException {
return read(false);
}
public boolean read(boolean wrappedLine) throws IOException {
this.escaped = false;
this.character = this.reader.read();
this.columnNumber++;
skipLeadingWhitespace();
skipComment();
if (this.columnNumber == 0) {
skipLeadingWhitespace();
if (!wrappedLine) {
skipComment();
}
}
if (this.character == '\\') {
this.escaped = true;
readEscaped();
@ -176,11 +185,9 @@ class OriginTrackedPropertiesLoader {
}
private void skipLeadingWhitespace() throws IOException {
if (this.columnNumber == 0) {
while (isWhiteSpace()) {
this.character = this.reader.read();
this.columnNumber++;
}
while (isWhiteSpace()) {
this.character = this.reader.read();
this.columnNumber++;
}
}
@ -202,7 +209,7 @@ class OriginTrackedPropertiesLoader {
}
else if (this.character == '\n') {
this.columnNumber = -1;
read();
read(true);
}
else if (this.character == 'u') {
readUnicode();

View File

@ -52,7 +52,7 @@ public class OriginTrackedPropertiesLoaderTests {
Properties ours = new Properties();
new OriginTrackedPropertiesLoader(this.resource).load(false)
.forEach((k, v) -> ours.put(k, v.getValue()));
assertThat(java).isEqualTo(ours);
assertThat(ours).isEqualTo(java);
}
@Test
@ -97,6 +97,20 @@ public class OriginTrackedPropertiesLoaderTests {
assertThat(getLocation(value)).isEqualTo("16:19");
}
@Test
public void getPropertyWithBang() throws Exception {
OriginTrackedValue value = this.properties.get("test-bang-property");
assertThat(getValue(value)).isEqualTo("foo!");
assertThat(getLocation(value)).isEqualTo("34:20");
}
@Test
public void getPropertyWithValueComment() throws Exception {
OriginTrackedValue value = this.properties.get("test-property-value-comment");
assertThat(getValue(value)).isEqualTo("foo !bar #foo");
assertThat(getLocation(value)).isEqualTo("36:29");
}
@Test
public void getPropertyWithCarriageReturn() throws Exception {
OriginTrackedValue value = this.properties.get("test-return-property");

View File

@ -31,3 +31,9 @@ language[pascal]=Lame
test-multiline-immediate=\
foo
!commented-two=bang\
test-bang-property=foo!
another=bar
test-property-value-comment=foo \
!bar #foo
test-multiline-immediate-bang=\
!foo