Commit | Line | Data |
---|---|---|
ec1f3444 NR |
1 | package be.nikiroo.utils.resources; |
2 | ||
3 | import java.io.BufferedWriter; | |
4 | import java.io.File; | |
5 | import java.io.FileInputStream; | |
6 | import java.io.FileOutputStream; | |
7 | import java.io.IOException; | |
8 | import java.io.InputStreamReader; | |
9 | import java.io.OutputStreamWriter; | |
10 | import java.io.Reader; | |
ec1f3444 NR |
11 | import java.io.Writer; |
12 | import java.lang.reflect.Field; | |
13 | import java.util.ArrayList; | |
62c9ec78 | 14 | import java.util.HashMap; |
ec1f3444 NR |
15 | import java.util.List; |
16 | import java.util.Locale; | |
62c9ec78 | 17 | import java.util.Map; |
ec1f3444 NR |
18 | import java.util.MissingResourceException; |
19 | import java.util.PropertyResourceBundle; | |
20 | import java.util.ResourceBundle; | |
21 | ||
b15a4985 NR |
22 | import be.nikiroo.utils.resources.Meta.Format; |
23 | ||
ec1f3444 | 24 | /** |
62c9ec78 | 25 | * This class encapsulate a {@link ResourceBundle} in UTF-8. It allows to |
ec1f3444 NR |
26 | * retrieve values associated to an enumeration, and allows some additional |
27 | * methods. | |
62c9ec78 NR |
28 | * <p> |
29 | * It also sports a writable change map, and you can save back the | |
30 | * {@link Bundle} to file with {@link Bundle#updateFile(String)}. | |
ec1f3444 | 31 | * |
ec1f3444 NR |
32 | * @param <E> |
33 | * the enum to use to get values out of this class | |
db31c358 NR |
34 | * |
35 | * @author niki | |
ec1f3444 | 36 | */ |
db31c358 | 37 | |
ec1f3444 | 38 | public class Bundle<E extends Enum<E>> { |
db31c358 | 39 | /** The type of E. */ |
ec1f3444 | 40 | protected Class<E> type; |
db31c358 NR |
41 | /** |
42 | * The {@link Enum} associated to this {@link Bundle} (all the keys used in | |
43 | * this {@link Bundle} will be of this type). | |
44 | */ | |
45 | protected Enum<?> keyType; | |
46 | ||
47 | private TransBundle<E> descriptionBundle; | |
48 | ||
49 | /** R/O map */ | |
50 | private Map<String, String> map; | |
51 | /** R/W map */ | |
52 | private Map<String, String> changeMap; | |
ec1f3444 NR |
53 | |
54 | /** | |
55 | * Create a new {@link Bundles} of the given name. | |
56 | * | |
57 | * @param type | |
58 | * a runtime instance of the class of E | |
ec1f3444 NR |
59 | * @param name |
60 | * the name of the {@link Bundles} | |
db31c358 NR |
61 | * @param descriptionBundle |
62 | * the description {@link TransBundle}, that is, a | |
63 | * {@link TransBundle} dedicated to the description of the values | |
64 | * of the given {@link Bundle} (can be NULL) | |
ec1f3444 | 65 | */ |
db31c358 NR |
66 | protected Bundle(Class<E> type, Enum<?> name, |
67 | TransBundle<E> descriptionBundle) { | |
ec1f3444 | 68 | this.type = type; |
db31c358 NR |
69 | this.keyType = name; |
70 | this.descriptionBundle = descriptionBundle; | |
71 | ||
487926f7 | 72 | this.map = new HashMap<String, String>(); |
62c9ec78 | 73 | this.changeMap = new HashMap<String, String>(); |
e9ca6bb8 | 74 | setBundle(name, Locale.getDefault(), false); |
ec1f3444 NR |
75 | } |
76 | ||
13bfeea6 NR |
77 | /** |
78 | * Check if the setting is set into this {@link Bundle}. | |
79 | * | |
80 | * @param id | |
81 | * the id of the setting to check | |
82 | * @param includeDefaultValue | |
83 | * TRUE to only return false when the setting is not set AND | |
84 | * there is no default value | |
85 | * | |
86 | * @return TRUE if the setting is set | |
87 | */ | |
b15a4985 NR |
88 | public boolean isSet(E id, boolean includeDefaultValue) { |
89 | return isSet(id.name(), includeDefaultValue); | |
90 | } | |
91 | ||
92 | /** | |
93 | * Check if the setting is set into this {@link Bundle}. | |
94 | * | |
856f5898 | 95 | * @param name |
b15a4985 NR |
96 | * the id of the setting to check |
97 | * @param includeDefaultValue | |
98 | * TRUE to only return false when the setting is not set AND | |
99 | * there is no default value | |
100 | * | |
101 | * @return TRUE if the setting is set | |
102 | */ | |
103 | protected boolean isSet(String name, boolean includeDefaultValue) { | |
104 | if (getString(name, null) == null) { | |
105 | if (!includeDefaultValue || getString(name, "") == null) { | |
13bfeea6 NR |
106 | return false; |
107 | } | |
108 | } | |
109 | ||
110 | return true; | |
111 | } | |
112 | ||
ec1f3444 NR |
113 | /** |
114 | * Return the value associated to the given id as a {@link String}. | |
115 | * | |
db31c358 | 116 | * @param id |
ec1f3444 NR |
117 | * the id of the value to get |
118 | * | |
119 | * @return the associated value, or NULL if not found (not present in the | |
120 | * resource file) | |
121 | */ | |
122 | public String getString(E id) { | |
13bfeea6 NR |
123 | return getString(id, null); |
124 | } | |
125 | ||
126 | /** | |
127 | * Return the value associated to the given id as a {@link String}. | |
128 | * <p> | |
129 | * If no value is associated, take the default one if any. | |
130 | * | |
131 | * @param id | |
132 | * the id of the value to get | |
133 | * @param def | |
134 | * the default value when it is not present in the config file | |
135 | * | |
136 | * @return the associated value, or NULL if not found (not present in the | |
137 | * resource file) | |
138 | */ | |
139 | public String getString(E id, String def) { | |
d5026c09 NR |
140 | return getString(id, def, -1); |
141 | } | |
142 | ||
143 | /** | |
144 | * Return the value associated to the given id as a {@link String}. | |
145 | * <p> | |
24604392 NR |
146 | * If no value is associated (or if it is empty!), take the default one if |
147 | * any. | |
d5026c09 NR |
148 | * |
149 | * @param id | |
150 | * the id of the value to get | |
151 | * @param def | |
152 | * the default value when it is not present in the config file | |
153 | * @param item | |
154 | * the item number to get for an array of values, or -1 for | |
155 | * non-arrays | |
156 | * | |
157 | * @return the associated value, or NULL if not found (not present in the | |
158 | * resource file) | |
159 | */ | |
160 | public String getString(E id, String def, int item) { | |
13bfeea6 NR |
161 | String rep = getString(id.name(), null); |
162 | if (rep == null) { | |
b15a4985 NR |
163 | try { |
164 | Meta meta = type.getDeclaredField(id.name()).getAnnotation( | |
165 | Meta.class); | |
166 | rep = meta.def(); | |
167 | } catch (NoSuchFieldException e) { | |
168 | } catch (SecurityException e) { | |
169 | } | |
13bfeea6 NR |
170 | } |
171 | ||
d5026c09 NR |
172 | if (rep == null || rep.isEmpty()) { |
173 | return def; | |
174 | } | |
175 | ||
176 | if (item >= 0) { | |
177 | List<String> values = BundleHelper.parseList(rep, item); | |
178 | if (values != null && item < values.size()) { | |
179 | return values.get(item); | |
180 | } | |
181 | ||
182 | return null; | |
13bfeea6 NR |
183 | } |
184 | ||
185 | return rep; | |
ec1f3444 NR |
186 | } |
187 | ||
62c9ec78 NR |
188 | /** |
189 | * Set the value associated to the given id as a {@link String}. | |
190 | * | |
db31c358 | 191 | * @param id |
3bfc1d20 | 192 | * the id of the value to set |
62c9ec78 NR |
193 | * @param value |
194 | * the value | |
195 | * | |
196 | */ | |
197 | public void setString(E id, String value) { | |
487926f7 | 198 | setString(id.name(), value); |
62c9ec78 NR |
199 | } |
200 | ||
d5026c09 NR |
201 | /** |
202 | * Set the value associated to the given id as a {@link String}. | |
203 | * | |
204 | * @param id | |
205 | * the id of the value to set | |
206 | * @param value | |
207 | * the value | |
208 | * @param item | |
209 | * the item number to get for an array of values, or -1 for | |
210 | * non-arrays | |
211 | * | |
212 | */ | |
213 | public void setString(E id, String value, int item) { | |
214 | if (item < 0) { | |
215 | setString(id.name(), value); | |
216 | } else { | |
217 | List<String> values = getList(id); | |
856f5898 | 218 | setString(id.name(), BundleHelper.fromList(values, value, item)); |
d5026c09 NR |
219 | } |
220 | } | |
221 | ||
ec1f3444 NR |
222 | /** |
223 | * Return the value associated to the given id as a {@link String} suffixed | |
224 | * with the runtime value "_suffix" (that is, "_" and suffix). | |
487926f7 NR |
225 | * <p> |
226 | * Will only accept suffixes that form an existing id. | |
13bfeea6 NR |
227 | * <p> |
228 | * If no value is associated, take the default one if any. | |
ec1f3444 | 229 | * |
db31c358 | 230 | * @param id |
ec1f3444 NR |
231 | * the id of the value to get |
232 | * @param suffix | |
233 | * the runtime suffix | |
234 | * | |
235 | * @return the associated value, or NULL if not found (not present in the | |
236 | * resource file) | |
237 | */ | |
238 | public String getStringX(E id, String suffix) { | |
d5026c09 NR |
239 | return getStringX(id, suffix, null, -1); |
240 | } | |
241 | ||
242 | /** | |
243 | * Return the value associated to the given id as a {@link String} suffixed | |
244 | * with the runtime value "_suffix" (that is, "_" and suffix). | |
245 | * <p> | |
246 | * Will only accept suffixes that form an existing id. | |
247 | * <p> | |
248 | * If no value is associated, take the default one if any. | |
249 | * | |
250 | * @param id | |
251 | * the id of the value to get | |
252 | * @param suffix | |
253 | * the runtime suffix | |
254 | * @param def | |
255 | * the default value when it is not present in the config file | |
256 | * | |
257 | * @return the associated value, or NULL if not found (not present in the | |
258 | * resource file) | |
259 | */ | |
260 | public String getStringX(E id, String suffix, String def) { | |
261 | return getStringX(id, suffix, def, -1); | |
262 | } | |
263 | ||
264 | /** | |
265 | * Return the value associated to the given id as a {@link String} suffixed | |
266 | * with the runtime value "_suffix" (that is, "_" and suffix). | |
267 | * <p> | |
268 | * Will only accept suffixes that form an existing id. | |
269 | * <p> | |
270 | * If no value is associated, take the default one if any. | |
271 | * | |
272 | * @param id | |
273 | * the id of the value to get | |
274 | * @param suffix | |
275 | * the runtime suffix | |
d5026c09 NR |
276 | * @param def |
277 | * the default value when it is not present in the config file | |
278 | * @param item | |
279 | * the item number to get for an array of values, or -1 for | |
280 | * non-arrays | |
281 | * | |
282 | * @return the associated value, or NULL if not found (not present in the | |
283 | * resource file) | |
284 | */ | |
285 | public String getStringX(E id, String suffix, String def, int item) { | |
ec1f3444 | 286 | String key = id.name() |
80383c14 | 287 | + (suffix == null ? "" : "_" + suffix.toUpperCase()); |
ec1f3444 | 288 | |
487926f7 NR |
289 | try { |
290 | id = Enum.valueOf(type, key); | |
d5026c09 | 291 | return getString(id, def, item); |
487926f7 | 292 | } catch (IllegalArgumentException e) { |
ec1f3444 NR |
293 | } |
294 | ||
295 | return null; | |
296 | } | |
297 | ||
62c9ec78 NR |
298 | /** |
299 | * Set the value associated to the given id as a {@link String} suffixed | |
300 | * with the runtime value "_suffix" (that is, "_" and suffix). | |
487926f7 NR |
301 | * <p> |
302 | * Will only accept suffixes that form an existing id. | |
62c9ec78 | 303 | * |
db31c358 | 304 | * @param id |
3bfc1d20 | 305 | * the id of the value to set |
62c9ec78 NR |
306 | * @param suffix |
307 | * the runtime suffix | |
308 | * @param value | |
309 | * the value | |
310 | */ | |
311 | public void setStringX(E id, String suffix, String value) { | |
d5026c09 NR |
312 | setStringX(id, suffix, value, -1); |
313 | } | |
314 | ||
315 | /** | |
316 | * Set the value associated to the given id as a {@link String} suffixed | |
317 | * with the runtime value "_suffix" (that is, "_" and suffix). | |
318 | * <p> | |
319 | * Will only accept suffixes that form an existing id. | |
320 | * | |
321 | * @param id | |
322 | * the id of the value to set | |
323 | * @param suffix | |
324 | * the runtime suffix | |
325 | * @param value | |
326 | * the value | |
327 | * @param item | |
328 | * the item number to get for an array of values, or -1 for | |
329 | * non-arrays | |
330 | */ | |
331 | public void setStringX(E id, String suffix, String value, int item) { | |
62c9ec78 NR |
332 | String key = id.name() |
333 | + (suffix == null ? "" : "_" + suffix.toUpperCase()); | |
334 | ||
487926f7 NR |
335 | try { |
336 | id = Enum.valueOf(type, key); | |
d5026c09 | 337 | setString(id, value, item); |
487926f7 | 338 | } catch (IllegalArgumentException e) { |
487926f7 | 339 | } |
62c9ec78 NR |
340 | } |
341 | ||
ec1f3444 NR |
342 | /** |
343 | * Return the value associated to the given id as a {@link Boolean}. | |
13bfeea6 NR |
344 | * <p> |
345 | * If no value is associated, take the default one if any. | |
ec1f3444 | 346 | * |
db31c358 | 347 | * @param id |
ec1f3444 NR |
348 | * the id of the value to get |
349 | * | |
350 | * @return the associated value | |
351 | */ | |
352 | public Boolean getBoolean(E id) { | |
d5026c09 | 353 | return BundleHelper.parseBoolean(getString(id), -1); |
ec1f3444 NR |
354 | } |
355 | ||
356 | /** | |
62c9ec78 | 357 | * Return the value associated to the given id as a {@link Boolean}. |
13bfeea6 NR |
358 | * <p> |
359 | * If no value is associated, take the default one if any. | |
ec1f3444 | 360 | * |
db31c358 | 361 | * @param id |
ec1f3444 NR |
362 | * the id of the value to get |
363 | * @param def | |
364 | * the default value when it is not present in the config file or | |
365 | * if it is not a boolean value | |
366 | * | |
367 | * @return the associated value | |
368 | */ | |
369 | public boolean getBoolean(E id, boolean def) { | |
d5026c09 NR |
370 | Boolean value = getBoolean(id); |
371 | if (value != null) { | |
372 | return value; | |
373 | } | |
374 | ||
375 | return def; | |
376 | } | |
377 | ||
378 | /** | |
379 | * Return the value associated to the given id as a {@link Boolean}. | |
380 | * <p> | |
381 | * If no value is associated, take the default one if any. | |
382 | * | |
383 | * @param id | |
384 | * the id of the value to get | |
385 | * @param def | |
386 | * the default value when it is not present in the config file or | |
387 | * if it is not a boolean value | |
388 | * @param item | |
389 | * the item number to get for an array of values, or -1 for | |
390 | * non-arrays | |
391 | * | |
392 | * @return the associated value | |
393 | */ | |
394 | public Boolean getBoolean(E id, boolean def, int item) { | |
395 | String value = getString(id); | |
396 | if (value != null) { | |
397 | return BundleHelper.parseBoolean(value, item); | |
398 | } | |
ec1f3444 NR |
399 | |
400 | return def; | |
401 | } | |
d18e136e | 402 | |
3bfc1d20 NR |
403 | /** |
404 | * Set the value associated to the given id as a {@link Boolean}. | |
405 | * | |
406 | * @param id | |
407 | * the id of the value to set | |
408 | * @param value | |
409 | * the value | |
410 | * | |
411 | */ | |
412 | public void setBoolean(E id, boolean value) { | |
d5026c09 NR |
413 | setBoolean(id, value, -1); |
414 | } | |
415 | ||
416 | /** | |
417 | * Set the value associated to the given id as a {@link Boolean}. | |
418 | * | |
419 | * @param id | |
420 | * the id of the value to set | |
421 | * @param value | |
422 | * the value | |
423 | * @param item | |
424 | * the item number to get for an array of values, or -1 for | |
425 | * non-arrays | |
426 | * | |
427 | */ | |
428 | public void setBoolean(E id, boolean value, int item) { | |
429 | setString(id, BundleHelper.fromBoolean(value), item); | |
3bfc1d20 NR |
430 | } |
431 | ||
ec1f3444 NR |
432 | /** |
433 | * Return the value associated to the given id as an {@link Integer}. | |
13bfeea6 NR |
434 | * <p> |
435 | * If no value is associated, take the default one if any. | |
ec1f3444 | 436 | * |
db31c358 | 437 | * @param id |
ec1f3444 NR |
438 | * the id of the value to get |
439 | * | |
440 | * @return the associated value | |
441 | */ | |
442 | public Integer getInteger(E id) { | |
d5026c09 NR |
443 | String value = getString(id); |
444 | if (value != null) { | |
445 | return BundleHelper.parseInteger(value, -1); | |
446 | } | |
447 | ||
448 | return null; | |
ec1f3444 NR |
449 | } |
450 | ||
451 | /** | |
db31c358 | 452 | * Return the value associated to the given id as an int. |
13bfeea6 NR |
453 | * <p> |
454 | * If no value is associated, take the default one if any. | |
ec1f3444 | 455 | * |
db31c358 | 456 | * @param id |
ec1f3444 NR |
457 | * the id of the value to get |
458 | * @param def | |
459 | * the default value when it is not present in the config file or | |
460 | * if it is not a int value | |
461 | * | |
462 | * @return the associated value | |
463 | */ | |
464 | public int getInteger(E id, int def) { | |
d5026c09 NR |
465 | Integer value = getInteger(id); |
466 | if (value != null) { | |
467 | return value; | |
468 | } | |
469 | ||
470 | return def; | |
471 | } | |
472 | ||
473 | /** | |
474 | * Return the value associated to the given id as an int. | |
475 | * <p> | |
476 | * If no value is associated, take the default one if any. | |
477 | * | |
478 | * @param id | |
479 | * the id of the value to get | |
480 | * @param def | |
481 | * the default value when it is not present in the config file or | |
482 | * if it is not a int value | |
483 | * @param item | |
484 | * the item number to get for an array of values, or -1 for | |
485 | * non-arrays | |
486 | * | |
487 | * @return the associated value | |
488 | */ | |
489 | public Integer getInteger(E id, int def, int item) { | |
490 | String value = getString(id); | |
491 | if (value != null) { | |
492 | return BundleHelper.parseInteger(value, item); | |
493 | } | |
ec1f3444 NR |
494 | |
495 | return def; | |
496 | } | |
497 | ||
3bfc1d20 NR |
498 | /** |
499 | * Set the value associated to the given id as a {@link Integer}. | |
500 | * | |
501 | * @param id | |
502 | * the id of the value to set | |
503 | * @param value | |
504 | * the value | |
505 | * | |
506 | */ | |
507 | public void setInteger(E id, int value) { | |
d5026c09 NR |
508 | setInteger(id, value, -1); |
509 | } | |
510 | ||
511 | /** | |
512 | * Set the value associated to the given id as a {@link Integer}. | |
513 | * | |
514 | * @param id | |
515 | * the id of the value to set | |
516 | * @param value | |
517 | * the value | |
518 | * @param item | |
519 | * the item number to get for an array of values, or -1 for | |
520 | * non-arrays | |
521 | * | |
522 | */ | |
523 | public void setInteger(E id, int value, int item) { | |
524 | setString(id, BundleHelper.fromInteger(value), item); | |
3bfc1d20 | 525 | } |
d18e136e | 526 | |
ec1f3444 NR |
527 | /** |
528 | * Return the value associated to the given id as a {@link Character}. | |
13bfeea6 NR |
529 | * <p> |
530 | * If no value is associated, take the default one if any. | |
ec1f3444 | 531 | * |
db31c358 | 532 | * @param id |
ec1f3444 NR |
533 | * the id of the value to get |
534 | * | |
535 | * @return the associated value | |
536 | */ | |
62c9ec78 | 537 | public Character getCharacter(E id) { |
d5026c09 | 538 | return BundleHelper.parseCharacter(getString(id), -1); |
62c9ec78 NR |
539 | } |
540 | ||
541 | /** | |
542 | * Return the value associated to the given id as a {@link Character}. | |
13bfeea6 NR |
543 | * <p> |
544 | * If no value is associated, take the default one if any. | |
62c9ec78 | 545 | * |
db31c358 | 546 | * @param id |
62c9ec78 NR |
547 | * the id of the value to get |
548 | * @param def | |
549 | * the default value when it is not present in the config file or | |
550 | * if it is not a char value | |
551 | * | |
552 | * @return the associated value | |
553 | */ | |
554 | public char getCharacter(E id, char def) { | |
d5026c09 NR |
555 | Character value = getCharacter(id); |
556 | if (value != null) { | |
557 | return value; | |
558 | } | |
559 | ||
560 | return def; | |
561 | } | |
562 | ||
563 | /** | |
564 | * Return the value associated to the given id as a {@link Character}. | |
565 | * <p> | |
566 | * If no value is associated, take the default one if any. | |
567 | * | |
568 | * @param id | |
569 | * the id of the value to get | |
570 | * @param def | |
571 | * the default value when it is not present in the config file or | |
572 | * if it is not a char value | |
856f5898 NR |
573 | * @param item |
574 | * the item number to get for an array of values, or -1 for | |
575 | * non-arrays | |
d5026c09 NR |
576 | * |
577 | * @return the associated value | |
578 | */ | |
579 | public Character getCharacter(E id, char def, int item) { | |
580 | String value = getString(id); | |
581 | if (value != null) { | |
582 | return BundleHelper.parseCharacter(value, item); | |
583 | } | |
62c9ec78 NR |
584 | |
585 | return def; | |
ec1f3444 NR |
586 | } |
587 | ||
d5026c09 NR |
588 | /** |
589 | * Set the value associated to the given id as a {@link Character}. | |
590 | * | |
591 | * @param id | |
592 | * the id of the value to set | |
593 | * @param value | |
594 | * the value | |
595 | * | |
596 | */ | |
597 | public void setCharacter(E id, char value) { | |
598 | setCharacter(id, value, -1); | |
599 | } | |
600 | ||
601 | /** | |
602 | * Set the value associated to the given id as a {@link Character}. | |
603 | * | |
604 | * @param id | |
605 | * the id of the value to set | |
606 | * @param value | |
607 | * the value | |
608 | * @param item | |
609 | * the item number to get for an array of values, or -1 for | |
610 | * non-arrays | |
611 | * | |
612 | */ | |
613 | public void setCharacter(E id, char value, int item) { | |
614 | setString(id, BundleHelper.fromCharacter(value), item); | |
615 | } | |
616 | ||
b3aad1f9 | 617 | /** |
80500544 NR |
618 | * Return the value associated to the given id as a colour if it is found |
619 | * and can be parsed. | |
620 | * <p> | |
621 | * The returned value is an ARGB value. | |
13bfeea6 NR |
622 | * <p> |
623 | * If no value is associated, take the default one if any. | |
b3aad1f9 | 624 | * |
db31c358 NR |
625 | * @param id |
626 | * the id of the value to get | |
b3aad1f9 NR |
627 | * |
628 | * @return the associated value | |
629 | */ | |
80500544 | 630 | public Integer getColor(E id) { |
d5026c09 NR |
631 | return BundleHelper.parseColor(getString(id), -1); |
632 | } | |
633 | ||
634 | /** | |
635 | * Return the value associated to the given id as a colour if it is found | |
636 | * and can be parsed. | |
637 | * <p> | |
638 | * The returned value is an ARGB value. | |
639 | * <p> | |
640 | * If no value is associated, take the default one if any. | |
641 | * | |
642 | * @param id | |
643 | * the id of the value to get | |
856f5898 NR |
644 | * @param def |
645 | * the default value when it is not present in the config file or | |
646 | * if it is not a char value | |
d5026c09 NR |
647 | * |
648 | * @return the associated value | |
649 | */ | |
650 | public int getColor(E id, int def) { | |
651 | Integer value = getColor(id); | |
652 | if (value != null) { | |
653 | return value; | |
654 | } | |
655 | ||
656 | return def; | |
657 | } | |
658 | ||
659 | /** | |
660 | * Return the value associated to the given id as a colour if it is found | |
661 | * and can be parsed. | |
662 | * <p> | |
663 | * The returned value is an ARGB value. | |
664 | * <p> | |
665 | * If no value is associated, take the default one if any. | |
666 | * | |
667 | * @param id | |
668 | * the id of the value to get | |
856f5898 NR |
669 | * @param def |
670 | * the default value when it is not present in the config file or | |
671 | * if it is not a char value | |
672 | * @param item | |
673 | * the item number to get for an array of values, or -1 for | |
674 | * non-arrays | |
d5026c09 NR |
675 | * |
676 | * @return the associated value | |
677 | */ | |
678 | public Integer getColor(E id, int def, int item) { | |
679 | String value = getString(id); | |
680 | if (value != null) { | |
681 | return BundleHelper.parseColor(value, item); | |
682 | } | |
683 | ||
684 | return def; | |
b3aad1f9 NR |
685 | } |
686 | ||
62c9ec78 | 687 | /** |
80500544 NR |
688 | * Set the value associated to the given id as a colour. |
689 | * <p> | |
3bfc1d20 | 690 | * The value is a BGRA value. |
62c9ec78 | 691 | * |
db31c358 NR |
692 | * @param id |
693 | * the id of the value to set | |
694 | * @param color | |
80500544 | 695 | * the new colour |
62c9ec78 | 696 | */ |
80500544 | 697 | public void setColor(E id, Integer color) { |
d5026c09 NR |
698 | setColor(id, color, -1); |
699 | } | |
700 | ||
701 | /** | |
702 | * Set the value associated to the given id as a Color. | |
703 | * | |
704 | * @param id | |
705 | * the id of the value to set | |
706 | * @param value | |
707 | * the value | |
708 | * @param item | |
709 | * the item number to get for an array of values, or -1 for | |
710 | * non-arrays | |
711 | * | |
712 | */ | |
713 | public void setColor(E id, int value, int item) { | |
714 | setString(id, BundleHelper.fromColor(value), item); | |
62c9ec78 NR |
715 | } |
716 | ||
7c192d3c NR |
717 | /** |
718 | * Return the value associated to the given id as a list of values if it is | |
719 | * found and can be parsed. | |
13bfeea6 NR |
720 | * <p> |
721 | * If no value is associated, take the default one if any. | |
7c192d3c NR |
722 | * |
723 | * @param id | |
724 | * the id of the value to get | |
725 | * | |
726 | * @return the associated list, empty if the value is empty, NULL if it is | |
727 | * not found or cannot be parsed as a list | |
728 | */ | |
729 | public List<String> getList(E id) { | |
d5026c09 NR |
730 | return BundleHelper.parseList(getString(id), -1); |
731 | } | |
732 | ||
733 | /** | |
734 | * Return the value associated to the given id as a list of values if it is | |
735 | * found and can be parsed. | |
736 | * <p> | |
737 | * If no value is associated, take the default one if any. | |
738 | * | |
739 | * @param id | |
740 | * the id of the value to get | |
856f5898 NR |
741 | * @param def |
742 | * the default value when it is not present in the config file or | |
743 | * if it is not a char value | |
d5026c09 NR |
744 | * |
745 | * @return the associated list, empty if the value is empty, NULL if it is | |
746 | * not found or cannot be parsed as a list | |
747 | */ | |
748 | public List<String> getList(E id, List<String> def) { | |
749 | List<String> value = getList(id); | |
750 | if (value != null) { | |
751 | return value; | |
752 | } | |
753 | ||
754 | return def; | |
755 | } | |
756 | ||
757 | /** | |
758 | * Return the value associated to the given id as a list of values if it is | |
759 | * found and can be parsed. | |
760 | * <p> | |
761 | * If no value is associated, take the default one if any. | |
762 | * | |
763 | * @param id | |
764 | * the id of the value to get | |
856f5898 NR |
765 | * @param def |
766 | * the default value when it is not present in the config file or | |
767 | * if it is not a char value | |
768 | * @param item | |
769 | * the item number to get for an array of values, or -1 for | |
770 | * non-arrays | |
d5026c09 NR |
771 | * |
772 | * @return the associated list, empty if the value is empty, NULL if it is | |
773 | * not found or cannot be parsed as a list | |
774 | */ | |
775 | public List<String> getList(E id, List<String> def, int item) { | |
776 | String value = getString(id); | |
777 | if (value != null) { | |
778 | return BundleHelper.parseList(value, item); | |
779 | } | |
780 | ||
781 | return def; | |
7c192d3c NR |
782 | } |
783 | ||
784 | /** | |
785 | * Set the value associated to the given id as a list of values. | |
786 | * | |
787 | * @param id | |
788 | * the id of the value to set | |
789 | * @param list | |
790 | * the new list of values | |
791 | */ | |
792 | public void setList(E id, List<String> list) { | |
d5026c09 NR |
793 | setList(id, list, -1); |
794 | } | |
795 | ||
796 | /** | |
797 | * Set the value associated to the given id as a {@link List}. | |
798 | * | |
799 | * @param id | |
800 | * the id of the value to set | |
801 | * @param value | |
802 | * the value | |
803 | * @param item | |
804 | * the item number to get for an array of values, or -1 for | |
805 | * non-arrays | |
806 | * | |
807 | */ | |
808 | public void setList(E id, List<String> value, int item) { | |
809 | setString(id, BundleHelper.fromList(value), item); | |
7c192d3c NR |
810 | } |
811 | ||
487926f7 NR |
812 | /** |
813 | * Create/update the .properties file. | |
814 | * <p> | |
815 | * Will use the most likely candidate as base if the file does not already | |
816 | * exists and this resource is translatable (for instance, "en_US" will use | |
817 | * "en" as a base if the resource is a translation file). | |
818 | * <p> | |
819 | * Will update the files in {@link Bundles#getDirectory()}; it <b>MUST</b> | |
820 | * be set. | |
821 | * | |
822 | * @throws IOException | |
823 | * in case of IO errors | |
824 | */ | |
825 | public void updateFile() throws IOException { | |
826 | updateFile(Bundles.getDirectory()); | |
827 | } | |
828 | ||
ec1f3444 | 829 | /** |
80383c14 NR |
830 | * Create/update the .properties file. |
831 | * <p> | |
832 | * Will use the most likely candidate as base if the file does not already | |
833 | * exists and this resource is translatable (for instance, "en_US" will use | |
834 | * "en" as a base if the resource is a translation file). | |
ec1f3444 NR |
835 | * |
836 | * @param path | |
487926f7 NR |
837 | * the path where the .properties files are, <b>MUST NOT</b> be |
838 | * NULL | |
ec1f3444 NR |
839 | * |
840 | * @throws IOException | |
841 | * in case of IO errors | |
842 | */ | |
843 | public void updateFile(String path) throws IOException { | |
844 | File file = getUpdateFile(path); | |
845 | ||
846 | BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( | |
847 | new FileOutputStream(file), "UTF-8")); | |
848 | ||
849 | writeHeader(writer); | |
850 | writer.write("\n"); | |
851 | writer.write("\n"); | |
852 | ||
853 | for (Field field : type.getDeclaredFields()) { | |
854 | Meta meta = field.getAnnotation(Meta.class); | |
855 | if (meta != null) { | |
cd0c27d2 | 856 | E id = Enum.valueOf(type, field.getName()); |
ec1f3444 NR |
857 | String info = getMetaInfo(meta); |
858 | ||
859 | if (info != null) { | |
860 | writer.write(info); | |
861 | writer.write("\n"); | |
862 | } | |
863 | ||
864 | writeValue(writer, id); | |
865 | } | |
866 | } | |
867 | ||
868 | writer.close(); | |
869 | } | |
870 | ||
e8aa5bf9 NR |
871 | /** |
872 | * Delete the .properties file. | |
873 | * <p> | |
874 | * Will use the most likely candidate as base if the file does not already | |
875 | * exists and this resource is translatable (for instance, "en_US" will use | |
876 | * "en" as a base if the resource is a translation file). | |
877 | * <p> | |
878 | * Will delete the files in {@link Bundles#getDirectory()}; it <b>MUST</b> | |
879 | * be set. | |
880 | * | |
881 | * @return TRUE if the file was deleted | |
882 | */ | |
883 | public boolean deleteFile() { | |
884 | return deleteFile(Bundles.getDirectory()); | |
885 | } | |
886 | ||
887 | /** | |
888 | * Delete the .properties file. | |
889 | * <p> | |
890 | * Will use the most likely candidate as base if the file does not already | |
891 | * exists and this resource is translatable (for instance, "en_US" will use | |
892 | * "en" as a base if the resource is a translation file). | |
893 | * | |
894 | * @param path | |
895 | * the path where the .properties files are, <b>MUST NOT</b> be | |
896 | * NULL | |
897 | * | |
898 | * @return TRUE if the file was deleted | |
899 | */ | |
900 | public boolean deleteFile(String path) { | |
901 | File file = getUpdateFile(path); | |
902 | return file.delete(); | |
903 | } | |
904 | ||
db31c358 NR |
905 | /** |
906 | * The description {@link TransBundle}, that is, a {@link TransBundle} | |
907 | * dedicated to the description of the values of the given {@link Bundle} | |
908 | * (can be NULL). | |
909 | * | |
910 | * @return the description {@link TransBundle} | |
911 | */ | |
912 | public TransBundle<E> getDescriptionBundle() { | |
913 | return descriptionBundle; | |
914 | } | |
915 | ||
80383c14 NR |
916 | /** |
917 | * Reload the {@link Bundle} data files. | |
e9ca6bb8 NR |
918 | * |
919 | * @param resetToDefault | |
920 | * reset to the default configuration (do not look into the | |
921 | * possible user configuration files, only take the original | |
922 | * configuration) | |
80383c14 | 923 | */ |
e9ca6bb8 | 924 | public void reload(boolean resetToDefault) { |
db31c358 | 925 | setBundle(keyType, Locale.getDefault(), resetToDefault); |
80383c14 NR |
926 | } |
927 | ||
ec1f3444 NR |
928 | /** |
929 | * Check if the internal map contains the given key. | |
930 | * | |
931 | * @param key | |
932 | * the key to check for | |
933 | * | |
934 | * @return true if it does | |
935 | */ | |
936 | protected boolean containsKey(String key) { | |
487926f7 | 937 | return changeMap.containsKey(key) || map.containsKey(key); |
ec1f3444 NR |
938 | } |
939 | ||
940 | /** | |
13bfeea6 NR |
941 | * Get the value for the given key if it exists in the internal map, or |
942 | * <tt>def</tt> if not. | |
ec1f3444 NR |
943 | * |
944 | * @param key | |
945 | * the key to check for | |
13bfeea6 NR |
946 | * @param def |
947 | * the default value when it is not present in the internal map | |
ec1f3444 | 948 | * |
13bfeea6 | 949 | * @return the value, or <tt>def</tt> if not found |
ec1f3444 | 950 | */ |
13bfeea6 | 951 | protected String getString(String key, String def) { |
62c9ec78 NR |
952 | if (changeMap.containsKey(key)) { |
953 | return changeMap.get(key); | |
954 | } | |
955 | ||
487926f7 NR |
956 | if (map.containsKey(key)) { |
957 | return map.get(key); | |
ec1f3444 NR |
958 | } |
959 | ||
13bfeea6 | 960 | return def; |
ec1f3444 NR |
961 | } |
962 | ||
62c9ec78 NR |
963 | /** |
964 | * Set the value for this key, in the change map (it is kept in memory, not | |
965 | * yet on disk). | |
966 | * | |
967 | * @param key | |
968 | * the key | |
969 | * @param value | |
970 | * the associated value | |
971 | */ | |
972 | protected void setString(String key, String value) { | |
487926f7 | 973 | changeMap.put(key, value == null ? null : value.trim()); |
62c9ec78 NR |
974 | } |
975 | ||
ec1f3444 NR |
976 | /** |
977 | * Return formated, display-able information from the {@link Meta} field | |
978 | * given. Each line will always starts with a "#" character. | |
979 | * | |
980 | * @param meta | |
981 | * the {@link Meta} field | |
982 | * | |
983 | * @return the information to display or NULL if none | |
984 | */ | |
985 | protected String getMetaInfo(Meta meta) { | |
db31c358 NR |
986 | String desc = meta.description(); |
987 | boolean group = meta.group(); | |
988 | Meta.Format format = meta.format(); | |
989 | String[] list = meta.list(); | |
990 | boolean nullable = meta.nullable(); | |
e8aa5bf9 | 991 | String def = meta.def(); |
db31c358 | 992 | boolean array = meta.array(); |
ec1f3444 | 993 | |
db31c358 | 994 | // Default, empty values -> NULL |
d18e136e | 995 | if (desc.length() + list.length + def.length() == 0 && !group |
b15a4985 | 996 | && nullable && format == Format.STRING) { |
ec1f3444 | 997 | return null; |
db31c358 | 998 | } |
ec1f3444 NR |
999 | |
1000 | StringBuilder builder = new StringBuilder(); | |
b15a4985 NR |
1001 | for (String line : desc.split("\n")) { |
1002 | builder.append("# ").append(line).append("\n"); | |
db31c358 | 1003 | } |
ec1f3444 | 1004 | |
db31c358 | 1005 | if (group) { |
b15a4985 | 1006 | builder.append("# This item is used as a group, its content is not expected to be used."); |
db31c358 | 1007 | } else { |
b15a4985 NR |
1008 | builder.append("# (FORMAT: ").append(format) |
1009 | .append(nullable ? "" : ", required"); | |
d18e136e | 1010 | builder.append(") "); |
db31c358 NR |
1011 | |
1012 | if (list.length > 0) { | |
b15a4985 NR |
1013 | builder.append("\n# ALLOWED VALUES: "); |
1014 | boolean first = true; | |
db31c358 | 1015 | for (String value : list) { |
b15a4985 NR |
1016 | if (!first) { |
1017 | builder.append(", "); | |
1018 | } | |
1019 | builder.append(BundleHelper.escape(value)); | |
1020 | first = false; | |
db31c358 | 1021 | } |
ec1f3444 NR |
1022 | } |
1023 | ||
db31c358 | 1024 | if (array) { |
d5026c09 | 1025 | builder.append("\n# (This item accepts a list of ^escaped comma-separated values)"); |
ec1f3444 NR |
1026 | } |
1027 | } | |
1028 | ||
ec1f3444 NR |
1029 | return builder.toString(); |
1030 | } | |
1031 | ||
1032 | /** | |
1033 | * The display name used in the <tt>.properties file</tt>. | |
1034 | * | |
1035 | * @return the name | |
1036 | */ | |
1037 | protected String getBundleDisplayName() { | |
db31c358 | 1038 | return keyType.toString(); |
ec1f3444 NR |
1039 | } |
1040 | ||
1041 | /** | |
1042 | * Write the header found in the configuration <tt>.properties</tt> file of | |
1043 | * this {@link Bundles}. | |
1044 | * | |
1045 | * @param writer | |
1046 | * the {@link Writer} to write the header in | |
1047 | * | |
1048 | * @throws IOException | |
1049 | * in case of IO error | |
1050 | */ | |
1051 | protected void writeHeader(Writer writer) throws IOException { | |
1052 | writer.write("# " + getBundleDisplayName() + "\n"); | |
1053 | writer.write("#\n"); | |
1054 | } | |
1055 | ||
1056 | /** | |
b15a4985 NR |
1057 | * Write the given data to the config file, i.e., "MY_ID = my_curent_value" |
1058 | * followed by a new line. | |
1059 | * <p> | |
1060 | * Will prepend a # sign if the is is not set (see | |
1061 | * {@link Bundle#isSet(Enum, boolean)}). | |
ec1f3444 NR |
1062 | * |
1063 | * @param writer | |
1064 | * the {@link Writer} to write into | |
1065 | * @param id | |
1066 | * the id to write | |
1067 | * | |
1068 | * @throws IOException | |
1069 | * in case of IO error | |
1070 | */ | |
1071 | protected void writeValue(Writer writer, E id) throws IOException { | |
b15a4985 NR |
1072 | boolean set = isSet(id, false); |
1073 | writeValue(writer, id.name(), getString(id), set); | |
ec1f3444 NR |
1074 | } |
1075 | ||
1076 | /** | |
1077 | * Write the given data to the config file, i.e., "MY_ID = my_curent_value" | |
b15a4985 NR |
1078 | * followed by a new line. |
1079 | * <p> | |
1080 | * Will prepend a # sign if the is is not set. | |
ec1f3444 NR |
1081 | * |
1082 | * @param writer | |
1083 | * the {@link Writer} to write into | |
1084 | * @param id | |
1085 | * the id to write | |
1086 | * @param value | |
1087 | * the id's value | |
b15a4985 NR |
1088 | * @param set |
1089 | * the value is set in this {@link Bundle} | |
ec1f3444 NR |
1090 | * |
1091 | * @throws IOException | |
1092 | * in case of IO error | |
1093 | */ | |
b15a4985 NR |
1094 | protected void writeValue(Writer writer, String id, String value, |
1095 | boolean set) throws IOException { | |
1096 | ||
1097 | if (!set) { | |
1098 | writer.write('#'); | |
1099 | } | |
1100 | ||
ec1f3444 NR |
1101 | writer.write(id); |
1102 | writer.write(" = "); | |
1103 | ||
1104 | if (value == null) { | |
1105 | value = ""; | |
1106 | } | |
1107 | ||
009196a4 | 1108 | String[] lines = value.replaceAll("\t", "\\\\\\t").split("\n"); |
ec1f3444 NR |
1109 | for (int i = 0; i < lines.length; i++) { |
1110 | writer.write(lines[i]); | |
1111 | if (i < lines.length - 1) { | |
1112 | writer.write("\\n\\"); | |
1113 | } | |
1114 | writer.write("\n"); | |
1115 | } | |
1116 | } | |
1117 | ||
1118 | /** | |
1119 | * Return the source file for this {@link Bundles} from the given path. | |
1120 | * | |
1121 | * @param path | |
1122 | * the path where the .properties files are | |
1123 | * | |
1124 | * @return the source {@link File} | |
ec1f3444 NR |
1125 | */ |
1126 | protected File getUpdateFile(String path) { | |
db31c358 | 1127 | return new File(path, keyType.name() + ".properties"); |
ec1f3444 NR |
1128 | } |
1129 | ||
1130 | /** | |
62c9ec78 | 1131 | * Change the currently used bundle, and reset all changes. |
ec1f3444 NR |
1132 | * |
1133 | * @param name | |
1134 | * the name of the bundle to load | |
1135 | * @param locale | |
1136 | * the {@link Locale} to use | |
e9ca6bb8 NR |
1137 | * @param resetToDefault |
1138 | * reset to the default configuration (do not look into the | |
1139 | * possible user configuration files, only take the original | |
1140 | * configuration) | |
ec1f3444 | 1141 | */ |
e9ca6bb8 | 1142 | protected void setBundle(Enum<?> name, Locale locale, boolean resetToDefault) { |
62c9ec78 | 1143 | changeMap.clear(); |
ec1f3444 | 1144 | String dir = Bundles.getDirectory(); |
e8aa5bf9 | 1145 | String bname = type.getPackage().getName() + "." + name.name(); |
ec1f3444 | 1146 | |
487926f7 | 1147 | boolean found = false; |
e9ca6bb8 | 1148 | if (!resetToDefault && dir != null) { |
e8aa5bf9 | 1149 | // Look into Bundles.getDirectory() for .properties files |
ec1f3444 NR |
1150 | try { |
1151 | File file = getPropertyFile(dir, name.name(), locale); | |
1152 | if (file != null) { | |
1153 | Reader reader = new InputStreamReader(new FileInputStream( | |
b15a4985 | 1154 | file), "UTF-8"); |
487926f7 NR |
1155 | resetMap(new PropertyResourceBundle(reader)); |
1156 | found = true; | |
ec1f3444 NR |
1157 | } |
1158 | } catch (IOException e) { | |
1159 | e.printStackTrace(); | |
1160 | } | |
1161 | } | |
1162 | ||
487926f7 | 1163 | if (!found) { |
e8aa5bf9 | 1164 | // Look into the package itself for resources |
e9ca6bb8 | 1165 | try { |
487926f7 NR |
1166 | resetMap(ResourceBundle |
1167 | .getBundle(bname, locale, type.getClassLoader(), | |
1168 | new FixedResourceBundleControl())); | |
e8aa5bf9 NR |
1169 | found = true; |
1170 | } catch (MissingResourceException e) { | |
e9ca6bb8 | 1171 | } catch (Exception e) { |
e8aa5bf9 | 1172 | e.printStackTrace(); |
487926f7 NR |
1173 | } |
1174 | } | |
e8aa5bf9 NR |
1175 | |
1176 | if (!found) { | |
1177 | // We have no bundle for this Bundle | |
1178 | System.err.println("No bundle found for: " + bname); | |
1179 | resetMap(null); | |
1180 | } | |
487926f7 NR |
1181 | } |
1182 | ||
1183 | /** | |
efba9850 | 1184 | * Reset the backing map to the content of the given bundle, or with NULL |
b15a4985 | 1185 | * values if bundle is NULL. |
487926f7 NR |
1186 | * |
1187 | * @param bundle | |
1188 | * the bundle to copy | |
1189 | */ | |
b771aed5 | 1190 | protected void resetMap(ResourceBundle bundle) { |
487926f7 | 1191 | this.map.clear(); |
e8aa5bf9 NR |
1192 | for (Field field : type.getDeclaredFields()) { |
1193 | try { | |
1194 | Meta meta = field.getAnnotation(Meta.class); | |
1195 | if (meta != null) { | |
1196 | E id = Enum.valueOf(type, field.getName()); | |
1197 | ||
1198 | String value; | |
1199 | if (bundle != null) { | |
1200 | value = bundle.getString(id.name()); | |
1201 | } else { | |
efba9850 | 1202 | value = null; |
e8aa5bf9 NR |
1203 | } |
1204 | ||
1205 | this.map.put(id.name(), value == null ? null : value.trim()); | |
487926f7 | 1206 | } |
e8aa5bf9 | 1207 | } catch (MissingResourceException e) { |
e9ca6bb8 NR |
1208 | } |
1209 | } | |
1210 | } | |
cd0c27d2 | 1211 | |
e9ca6bb8 NR |
1212 | /** |
1213 | * Take a snapshot of the changes in memory in this {@link Bundle} made by | |
1214 | * the "set" methods ( {@link Bundle#setString(Enum, String)}...) at the | |
1215 | * current time. | |
1216 | * | |
487926f7 | 1217 | * @return a snapshot to use with {@link Bundle#restoreSnapshot(Object)} |
e9ca6bb8 | 1218 | */ |
487926f7 | 1219 | public Object takeSnapshot() { |
e9ca6bb8 NR |
1220 | return new HashMap<String, String>(changeMap); |
1221 | } | |
1222 | ||
1223 | /** | |
1224 | * Restore a snapshot taken with {@link Bundle}, or reset the current | |
1225 | * changes if the snapshot is NULL. | |
1226 | * | |
1227 | * @param snap | |
1228 | * the snapshot or NULL | |
1229 | */ | |
1230 | @SuppressWarnings("unchecked") | |
487926f7 | 1231 | public void restoreSnapshot(Object snap) { |
e9ca6bb8 NR |
1232 | if (snap == null) { |
1233 | changeMap.clear(); | |
1234 | } else { | |
1235 | if (snap instanceof Map) { | |
1236 | changeMap = (Map<String, String>) snap; | |
1237 | } else { | |
d827da2a | 1238 | throw new RuntimeException( |
e9ca6bb8 NR |
1239 | "Restoring changes in a Bundle must be done on a changes snapshot, " |
1240 | + "or NULL to discard current changes"); | |
1241 | } | |
ec1f3444 NR |
1242 | } |
1243 | } | |
1244 | ||
1245 | /** | |
1246 | * Return the resource file that is closer to the {@link Locale}. | |
1247 | * | |
1248 | * @param dir | |
db31c358 | 1249 | * the directory to look into |
ec1f3444 | 1250 | * @param name |
db31c358 | 1251 | * the file base name (without <tt>.properties</tt>) |
ec1f3444 NR |
1252 | * @param locale |
1253 | * the {@link Locale} | |
1254 | * | |
1255 | * @return the closest match or NULL if none | |
1256 | */ | |
1257 | private File getPropertyFile(String dir, String name, Locale locale) { | |
1258 | List<String> locales = new ArrayList<String>(); | |
1259 | if (locale != null) { | |
1260 | String country = locale.getCountry() == null ? "" : locale | |
1261 | .getCountry(); | |
1262 | String language = locale.getLanguage() == null ? "" : locale | |
1263 | .getLanguage(); | |
1264 | if (!language.isEmpty() && !country.isEmpty()) { | |
1265 | locales.add("_" + language + "-" + country); | |
1266 | } | |
1267 | if (!language.isEmpty()) { | |
1268 | locales.add("_" + language); | |
1269 | } | |
1270 | } | |
1271 | ||
1272 | locales.add(""); | |
1273 | ||
1274 | File file = null; | |
1275 | for (String loc : locales) { | |
1276 | file = new File(dir, name + loc + ".properties"); | |
1277 | if (file.exists()) { | |
1278 | break; | |
ec1f3444 | 1279 | } |
d827da2a | 1280 | |
cd0c27d2 | 1281 | file = null; |
ec1f3444 NR |
1282 | } |
1283 | ||
1284 | return file; | |
1285 | } | |
1286 | } |