1717
1818package pl .project13 .core ;
1919
20- import com .fasterxml .jackson .core .type .TypeReference ;
21- import com .fasterxml .jackson .databind .MapperFeature ;
22- import com .fasterxml .jackson .databind .ObjectMapper ;
2320import nu .studer .java .util .OrderedProperties ;
2421import org .sonatype .plexus .build .incremental .BuildContext ;
2522import pl .project13 .core .log .LoggerBridge ;
23+ import pl .project13 .core .util .JsonManager ;
2624
2725import javax .annotation .Nonnull ;
2826import java .io .*;
2927import java .nio .charset .Charset ;
3028import java .nio .file .Files ;
3129import java .util .Comparator ;
32- import java .util .HashMap ;
33- import java .util .Map ;
3430import java .util .Properties ;
3531
3632public class PropertiesFileGenerator {
37- private static final ObjectMapper MAPPER = new ObjectMapper ().enable (MapperFeature .BLOCK_UNSAFE_POLYMORPHIC_BASE_TYPES );
3833
3934 private LoggerBridge log ;
4035 private BuildContext buildContext ;
@@ -63,11 +58,9 @@ public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, Fil
6358 try {
6459 if (isJsonFormat ) {
6560 log .info ("Reading existing json file [{}] (for module {})..." , gitPropsFile .getAbsolutePath (), projectName );
66-
67- persistedProperties = readJsonProperties (gitPropsFile , sourceCharset );
61+ persistedProperties = JsonManager .readJsonProperties (gitPropsFile , sourceCharset );
6862 } else {
6963 log .info ("Reading existing properties file [{}] (for module {})..." , gitPropsFile .getAbsolutePath (), projectName );
70-
7164 persistedProperties = readProperties (gitPropsFile , sourceCharset );
7265 }
7366
@@ -89,16 +82,11 @@ public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, Fil
8982 if (shouldGenerate ) {
9083 Files .createDirectories (gitPropsFile .getParentFile ().toPath ());
9184 try (OutputStream outputStream = new FileOutputStream (gitPropsFile )) {
92- OrderedProperties sortedLocalProperties = new OrderedProperties .OrderedPropertiesBuilder ()
93- .withSuppressDateInComment (true )
94- .withOrdering (Comparator .nullsLast (Comparator .naturalOrder ()))
95- .build ();
85+ OrderedProperties sortedLocalProperties = PropertiesFileGenerator .createOrderedProperties ();
9686 localProperties .forEach ((key , value ) -> sortedLocalProperties .setProperty ((String ) key , (String ) value ));
9787 if (isJsonFormat ) {
98- try (Writer outputWriter = new OutputStreamWriter (outputStream , sourceCharset )) {
99- log .info ("Writing json file to [{}] (for module {})..." , gitPropsFile .getAbsolutePath (), projectName );
100- MAPPER .writerWithDefaultPrettyPrinter ().writeValue (outputWriter , sortedLocalProperties );
101- }
88+ log .info ("Writing json file to [{}] (for module {})..." , gitPropsFile .getAbsolutePath (), projectName );
89+ JsonManager .dumpJson (outputStream , sortedLocalProperties , sourceCharset );
10290 } else {
10391 log .info ("Writing properties file to [{}] (for module {})..." , gitPropsFile .getAbsolutePath (), projectName );
10492 // using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
@@ -120,6 +108,13 @@ public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, Fil
120108 }
121109 }
122110
111+ public static OrderedProperties createOrderedProperties () {
112+ return new OrderedProperties .OrderedPropertiesBuilder ()
113+ .withSuppressDateInComment (true )
114+ .withOrdering (Comparator .nullsLast (Comparator .naturalOrder ()))
115+ .build ();
116+ }
117+
123118 public static File craftPropertiesOutputFile (File base , String propertiesFilename ) {
124119 File returnPath = new File (base , propertiesFilename );
125120
@@ -131,29 +126,6 @@ public static File craftPropertiesOutputFile(File base, String propertiesFilenam
131126 return returnPath ;
132127 }
133128
134- private Properties readJsonProperties (@ Nonnull File jsonFile , Charset sourceCharset ) throws CannotReadFileException {
135- final HashMap <String , Object > propertiesMap ;
136-
137- try (final FileInputStream fis = new FileInputStream (jsonFile )) {
138- try (final InputStreamReader reader = new InputStreamReader (fis , sourceCharset )) {
139- final TypeReference <HashMap <String , Object >> mapTypeRef =
140- new TypeReference <HashMap <String , Object >>() {};
141-
142- propertiesMap = MAPPER .readValue (reader , mapTypeRef );
143- }
144- } catch (final Exception ex ) {
145- throw new CannotReadFileException (ex );
146- }
147-
148- final Properties retVal = new Properties ();
149-
150- for (final Map .Entry <String , Object > entry : propertiesMap .entrySet ()) {
151- retVal .setProperty (entry .getKey (), String .valueOf (entry .getValue ()));
152- }
153-
154- return retVal ;
155- }
156-
157129 private Properties readProperties (@ Nonnull File propertiesFile , Charset sourceCharset ) throws CannotReadFileException {
158130 try (final FileInputStream fis = new FileInputStream (propertiesFile )) {
159131 try (final InputStreamReader reader = new InputStreamReader (fis , sourceCharset )) {
@@ -165,12 +137,4 @@ private Properties readProperties(@Nonnull File propertiesFile, Charset sourceCh
165137 throw new CannotReadFileException (ex );
166138 }
167139 }
168-
169- static class CannotReadFileException extends Exception {
170- private static final long serialVersionUID = -6290782570018307756L ;
171-
172- CannotReadFileException (Throwable cause ) {
173- super (cause );
174- }
175- }
176140}
0 commit comments