Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit 7b44355

Browse files
SORMAS-Foundation#2673 keep line break style + UTF-8 encoding
1 parent 05665a1 commit 7b44355

2 files changed

Lines changed: 45 additions & 13 deletions

File tree

sormas-api/src/test/java/de/symeda/sormas/api/i18n/I18nConstantGenerator.java

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package de.symeda.sormas.api.i18n;
22

3-
import java.io.FileWriter;
3+
import java.io.FileNotFoundException;
44
import java.io.IOException;
55
import java.io.InputStream;
6+
import java.io.UncheckedIOException;
67
import java.io.Writer;
8+
import java.nio.charset.StandardCharsets;
79
import java.nio.file.Files;
810
import java.nio.file.Path;
911
import java.nio.file.Paths;
@@ -13,8 +15,11 @@
1315
import java.util.Enumeration;
1416
import java.util.List;
1517
import java.util.Properties;
18+
import java.util.Scanner;
1619
import java.util.TreeSet;
1720

21+
import org.apache.commons.lang3.StringUtils;
22+
1823
/**
1924
* Generates Constants out of the corresponding property files.
2025
*
@@ -73,8 +78,29 @@ public boolean isIgnoreChildren() {
7378

7479
private void generateI18nConstantClass() throws IOException {
7580

76-
Writer writer = new FileWriter(outputClassFilePath, false);
77-
writeI18nConstantClass(writer);
81+
Path path = Paths.get(outputClassFilePath);
82+
String sep = determineLineSeparator(path);
83+
84+
try (Writer writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
85+
writeI18nConstantClass(writer, sep);
86+
}
87+
}
88+
89+
/**
90+
* Try to determine line separator from file
91+
*/
92+
private static String determineLineSeparator(Path path) {
93+
if (Files.exists(path)) {
94+
try (Scanner s = new Scanner(path.toFile())) {
95+
String sep = s.findWithinHorizon("\\R", 0);
96+
if (StringUtils.isNoneEmpty(sep)) {
97+
return sep;
98+
}
99+
} catch (FileNotFoundException e) {
100+
throw new UncheckedIOException(e);
101+
}
102+
}
103+
return System.lineSeparator();
78104
}
79105

80106
/**
@@ -87,7 +113,7 @@ private void generateI18nConstantClass() throws IOException {
87113
* Writes the java file into this {@code writer}.
88114
* @throws IOException
89115
*/
90-
void writeI18nConstantClass(Writer writer) throws IOException {
116+
void writeI18nConstantClass(Writer writer, String sep) throws IOException {
91117

92118
Properties properties = new Properties();
93119
InputStream inputStream = I18nProperties.class.getClassLoader().getResourceAsStream(propertiesFileName);
@@ -97,12 +123,18 @@ void writeI18nConstantClass(Writer writer) throws IOException {
97123

98124
Enumeration<?> e = properties.propertyNames();
99125

100-
writer.write("package de.symeda.sormas.api.i18n;\n\n");
101-
writer.write("import javax.annotation.Generated;\n\n");
102-
writer.write("@Generated(value = \"" + getClass().getCanonicalName() + "\")\n");
103-
writer.write("public interface " + outputClassName + " {\n\n");
104-
writer.write(
105-
"\t/*\n\t * Hint for SonarQube issues:\n\t * 1. java:S115: Violation of name convention for constants of this class is accepted: Close as false positive.\n\t */\n\n");
126+
writer.append("package de.symeda.sormas.api.i18n;").append(sep + sep);
127+
writer.append("import javax.annotation.Generated;").append(sep + sep);
128+
writer.append("@Generated(value = \"" + getClass().getCanonicalName() + "\")").append(sep);
129+
writer.append("public interface " + outputClassName + " {").append(sep + sep);
130+
writer.append("\t/*")
131+
.append(sep)
132+
.append("\t * Hint for SonarQube issues:")
133+
.append(sep)
134+
.append("\t * 1. java:S115: Violation of name convention for constants of this class is accepted: Close as false positive.")
135+
.append(sep)
136+
.append("\t */")
137+
.append(sep + sep);
106138

107139
Collection<String> orderedKeys = new TreeSet<String>(new Comparator<String>() {
108140

@@ -120,10 +152,10 @@ public int compare(String s1, String s2) {
120152

121153
for (String key : orderedKeys) {
122154
String constant = key.replaceAll("[\\.\\-]", "_");
123-
writer.write("\tString " + constant + " = \"" + key + "\";\n");
155+
writer.append("\tString " + constant + " = \"" + key + "\";").append(sep);
124156
}
125157

126-
writer.write("}\n");
158+
writer.append("}").append(sep);
127159
writer.flush();
128160
writer.close();
129161
}

sormas-api/src/test/java/de/symeda/sormas/api/i18n/I18nConstantsUpdatedTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void testConstantsAreUpdated() throws IOException {
2626
List<String> invalid = new ArrayList<>();
2727
for (I18nConstantGenerator generator : config) {
2828
StringWriter writer = new StringWriter();
29-
generator.writeI18nConstantClass(writer);
29+
generator.writeI18nConstantClass(writer, "\n");
3030

3131
if (!isValidContent(generator, writer.toString())) {
3232
invalid.add(generator.getOutputClassFilePath());

0 commit comments

Comments
 (0)