Skip to content

Commit

Permalink
escape whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo committed Nov 27, 2024
1 parent 2076c71 commit b4ed34c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.text.StringEscapeUtils;
import org.apache.maven.api.Project;
import org.apache.maven.api.Session;
import org.codehaus.plexus.archiver.Archiver;
Expand Down Expand Up @@ -88,8 +88,23 @@ private void createPropertiesFile(Properties unsortedProperties, Path outputFile
}

private static String escape(String s) {
String escaped = StringEscapeUtils.escapeJava(s);
return escaped;
StringBuilder sb = new StringBuilder(s.length());
for (char c : s.toCharArray()) {
if (Character.isWhitespace(c) || c == '#' || c == '!' || c == '=' || c == ':') { // backslash escape
sb.append('\\');
sb.append(c);
} else if (c < 256) { // 8859-1
sb.append(c);
} else {
sb.append("\\u");
String hexString = Integer.toHexString(c).toUpperCase(Locale.ENGLISH);
while (hexString.length() < 4) {
hexString = '0' + hexString;
}
sb.append(hexString);
}
}
return sb.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.maven.shared.archiver;

import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -81,4 +82,36 @@ void testUnicodeEscape() throws IOException {
assertEquals("version=2.1.5", contents.get(2));
assertEquals(3, contents.size());
}

@Test
void testWhitespaceEscape() throws IOException {
Path pomPropertiesFile = tempDirectory.resolve("bar.properties");
Path customPomPropertiesFile = tempDirectory.resolve("custom.properties");
try (Writer out = Files.newBufferedWriter(customPomPropertiesFile, StandardCharsets.ISO_8859_1)) {
out.write("a\\u0020key\\u0020with\\u0009whitespace=value\\u0020with\\u0009whitespace\n");
}

util.createPomProperties(
(Session) null,
"org.foo",
"こんにちは",
"2.1.5",
new JarArchiver(),
customPomPropertiesFile,
pomPropertiesFile,
true);
assertThat(pomPropertiesFile).exists();

Properties actual = new Properties();
actual.load(Files.newInputStream(pomPropertiesFile));
assertEquals("value with\twhitespace", actual.getProperty("a key with\twhitespace"));

// Now read the file directly to check for alphabetical order and encoding
List<String> contents = Files.readAllLines(pomPropertiesFile, StandardCharsets.ISO_8859_1);
assertEquals(4, contents.size());
assertEquals("a\\ key\\ with\\\twhitespace=value\\ with\\\twhitespace", contents.get(0));
assertEquals("artifactId=\\u3053\\u3093\\u306B\\u3061\\u306F", contents.get(1));
assertEquals("groupId=org.foo", contents.get(2));
assertEquals("version=2.1.5", contents.get(3));
}
}

0 comments on commit b4ed34c

Please sign in to comment.