1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package fr.inrae.agroclim.indicators.resources;
18
19 import java.text.MessageFormat;
20 import java.time.LocalDateTime;
21 import java.time.format.DateTimeFormatter;
22 import java.time.format.DateTimeParseException;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Locale;
27 import java.util.MissingResourceException;
28 import java.util.Objects;
29 import java.util.ResourceBundle;
30
31 import lombok.Getter;
32
33
34
35
36
37
38
39
40
41 public final class Resources {
42
43
44
45
46 private Locale currentLocale;
47
48
49
50
51 @Getter
52 private String bundleName;
53
54
55
56
57 private ResourceBundle resourceBundle;
58
59
60
61
62
63
64 public Resources(final ResourceBundle bundle) {
65 bundleName = bundle.getBaseBundleName();
66 currentLocale = bundle.getLocale();
67 resourceBundle = bundle;
68 }
69
70
71
72
73
74
75
76 public Resources(final String name, final Locale locale) {
77 bundleName = name;
78 currentLocale = locale;
79 resourceBundle = ResourceBundle.getBundle(bundleName, currentLocale);
80
81 if (!resourceBundle.getLocale().equals(locale)) {
82 resourceBundle = ResourceBundle.getBundle(bundleName, Locale.ROOT);
83 }
84 }
85
86
87
88
89
90
91
92
93 public String format(final String key,
94 final Object... messageArguments) {
95 try {
96 final MessageFormat formatter = new MessageFormat(resourceBundle.getString(key), currentLocale);
97 return formatter.format(messageArguments);
98 } catch (final MissingResourceException e) {
99 if (messageArguments == null) {
100 return '!' + key + '!';
101 }
102 return '!' + key + " : " + Arrays.toString(messageArguments) + "!";
103 }
104 }
105
106
107
108
109 public List<String> getKeys() {
110 return Collections.list(resourceBundle.getKeys());
111 }
112
113
114
115
116
117
118
119
120
121
122
123 public LocalDateTime getLocalDateTime(final String key)
124 throws DateTimeParseException {
125 final String val = getString(key);
126 DateTimeFormatter formatter;
127 DateTimeParseException exception = null;
128 for (final String format : Arrays.asList("yyyyMMddHHmmss",
129 "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd HH:mm:ss")) {
130 try {
131 formatter = DateTimeFormatter.ofPattern(format);
132 return formatter.parse(getString(key), LocalDateTime::from);
133 } catch (final DateTimeParseException ex) {
134 exception = ex;
135 }
136 }
137 throw new DateTimeParseException(
138 String.format(
139 "Value of key \"%s\" could not be parsed: %s",
140 key,
141 val),
142 val,
143 0,
144 exception);
145 }
146
147
148
149
150
151
152
153 public String getString(final String key) {
154 try {
155 return resourceBundle.getString(key);
156 } catch (final MissingResourceException e) {
157 return '!' + key + '!';
158 }
159 }
160
161
162
163
164
165
166 public String getVersionAndBuildDate() {
167 final String version = getString("version");
168 final String date = getString("build.date")
169 .replace("-", "")
170 .replace(" ", "")
171 .replace(":", "");
172 return version + "+" + date;
173 }
174
175
176
177
178 private void refresh() {
179 resourceBundle = ResourceBundle.getBundle(bundleName, currentLocale);
180 }
181
182
183
184
185 public void setBundleName(final String name) {
186 bundleName = name;
187 refresh();
188 }
189
190
191
192
193 public void setLocale(final Locale locale) {
194 if (Objects.equals(currentLocale, locale)) {
195 return;
196 }
197 currentLocale = locale;
198 refresh();
199 }
200 }