24
24
import pl .project13 .core .PropertiesFileGenerator ;
25
25
import pl .project13 .core .log .LogInterface ;
26
26
27
+ import javax .annotation .Nonnull ;
28
+ import javax .annotation .Nullable ;
27
29
import java .io .File ;
28
30
import java .io .FileOutputStream ;
29
31
import java .io .IOException ;
34
36
35
37
public class GenericFileManager {
36
38
public static Properties readProperties (
37
- LogInterface log ,
38
- CommitIdPropertiesOutputFormat propertiesOutputFormat ,
39
- File gitPropsFile ,
40
- Charset sourceCharset ,
41
- String projectName
39
+ @ Nullable LogInterface log ,
40
+ @ Nonnull CommitIdPropertiesOutputFormat propertiesOutputFormat ,
41
+ @ Nonnull File gitPropsFile ,
42
+ @ Nonnull Charset sourceCharset ,
43
+ @ Nullable String projectName
42
44
) throws GitCommitIdExecutionException {
43
45
final Properties persistedProperties ;
44
46
45
47
try {
48
+ if (log != null ) {
49
+ log .info (String .format ("Reading existing %s file [%s] (for project %s)..." ,
50
+ propertiesOutputFormat .name ().toLowerCase (), gitPropsFile .getAbsolutePath (), projectName ));
51
+ }
46
52
switch (propertiesOutputFormat ) {
47
53
case JSON :
48
- log .info (String .format ("Reading existing json file [%s] (for project %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
49
54
persistedProperties = JsonManager .readJsonProperties (gitPropsFile , sourceCharset );
50
55
break ;
51
56
case PROPERTIES :
52
- log .info (String .format ("Reading existing properties file [%s] (for project %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
53
57
persistedProperties = PropertyManager .readProperties (gitPropsFile );
54
58
break ;
55
59
case XML :
56
- log .info (String .format ("Reading existing xml file [%s] (for project %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
57
60
persistedProperties = XmlManager .readXmlProperties (gitPropsFile , sourceCharset );
58
61
break ;
59
62
case YML :
60
- log .info (String .format ("Reading existing yml file [%s] (for project %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
61
63
persistedProperties = YmlManager .readYmlProperties (gitPropsFile , sourceCharset );
62
64
break ;
63
65
default :
@@ -66,42 +68,43 @@ public static Properties readProperties(
66
68
} catch (final CannotReadFileException ex ) {
67
69
// Read has failed, regenerate file
68
70
throw new GitCommitIdExecutionException (
69
- String .format ("Cannot read properties file [%s] (for project %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
71
+ String .format ("Cannot read file [%s] (for project %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
70
72
}
71
73
return persistedProperties ;
72
74
}
73
75
74
76
public static void dumpProperties (
75
- LogInterface log ,
76
- CommitIdPropertiesOutputFormat propertiesOutputFormat ,
77
- File gitPropsFile ,
78
- Charset sourceCharset ,
77
+ @ Nullable LogInterface log ,
78
+ @ Nonnull CommitIdPropertiesOutputFormat propertiesOutputFormat ,
79
+ @ Nonnull File gitPropsFile ,
80
+ @ Nonnull Charset sourceCharset ,
79
81
boolean escapeUnicode ,
80
- String projectName ,
81
- Properties propertiesToDump
82
+ @ Nullable String projectName ,
83
+ @ Nonnull Properties propertiesToDump
82
84
) throws GitCommitIdExecutionException {
83
85
try {
86
+ if (log != null ) {
87
+ log .info (String .format ("Writing %s file [%s] (for project %s)..." ,
88
+ propertiesOutputFormat .name ().toLowerCase (), gitPropsFile .getAbsolutePath (), projectName ));
89
+ }
90
+
84
91
Files .createDirectories (gitPropsFile .getParentFile ().toPath ());
85
92
try (final OutputStream outputStream = new FileOutputStream (gitPropsFile )) {
86
93
OrderedProperties sortedLocalProperties = PropertiesFileGenerator .createOrderedProperties ();
87
94
propertiesToDump .forEach ((key , value ) -> sortedLocalProperties .setProperty ((String ) key , (String ) value ));
88
95
switch (propertiesOutputFormat ) {
89
96
case JSON :
90
- log .info (String .format ("Writing json file to [%s] (for project %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
91
97
JsonManager .dumpJson (outputStream , sortedLocalProperties , sourceCharset );
92
98
break ;
93
99
case PROPERTIES :
94
- log .info (String .format ("Writing properties file to [%s] (for project %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
95
100
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
96
101
PropertyManager .dumpProperties (outputStream , sortedLocalProperties , escapeUnicode );
97
102
break ;
98
103
case XML :
99
- log .info (String .format ("Writing xml file to [%s] (for project %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
100
104
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
101
105
XmlManager .dumpXml (outputStream , sortedLocalProperties , sourceCharset );
102
106
break ;
103
107
case YML :
104
- log .info (String .format ("Writing yml file to [%s] (for project %s)..." , gitPropsFile .getAbsolutePath (), projectName ));
105
108
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
106
109
YmlManager .dumpYml (outputStream , sortedLocalProperties , sourceCharset );
107
110
break ;
0 commit comments