11package de .symeda .sormas .api .i18n ;
22
3- import java .io .FileWriter ;
3+ import java .io .FileNotFoundException ;
44import java .io .IOException ;
55import java .io .InputStream ;
6+ import java .io .UncheckedIOException ;
67import java .io .Writer ;
8+ import java .nio .charset .StandardCharsets ;
79import java .nio .file .Files ;
810import java .nio .file .Path ;
911import java .nio .file .Paths ;
1315import java .util .Enumeration ;
1416import java .util .List ;
1517import java .util .Properties ;
18+ import java .util .Scanner ;
1619import 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 ("\t String " + constant + " = \" " + key + "\" ;\n " );
155+ writer .append ("\t String " + constant + " = \" " + key + "\" ;" ). append ( sep );
124156 }
125157
126- writer .write ("}\n " );
158+ writer .append ("}" ). append ( sep );
127159 writer .flush ();
128160 writer .close ();
129161 }
0 commit comments