1 package be
.nikiroo
.utils
.resources
;
3 import java
.io
.BufferedWriter
;
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
;
11 import java
.io
.Writer
;
12 import java
.lang
.reflect
.Field
;
13 import java
.util
.ArrayList
;
14 import java
.util
.HashMap
;
15 import java
.util
.List
;
16 import java
.util
.Locale
;
18 import java
.util
.MissingResourceException
;
19 import java
.util
.PropertyResourceBundle
;
20 import java
.util
.ResourceBundle
;
22 import be
.nikiroo
.utils
.resources
.Meta
.Format
;
25 * This class encapsulate a {@link ResourceBundle} in UTF-8. It allows to
26 * retrieve values associated to an enumeration, and allows some additional
29 * It also sports a writable change map, and you can save back the
30 * {@link Bundle} to file with {@link Bundle#updateFile(String)}.
33 * the enum to use to get values out of this class
38 public class Bundle
<E
extends Enum
<E
>> {
40 protected Class
<E
> type
;
42 * The {@link Enum} associated to this {@link Bundle} (all the keys used in
43 * this {@link Bundle} will be of this type).
45 protected Enum
<?
> keyType
;
47 private TransBundle
<E
> descriptionBundle
;
50 private Map
<String
, String
> map
;
52 private Map
<String
, String
> changeMap
;
55 * Create a new {@link Bundles} of the given name.
58 * a runtime instance of the class of E
60 * the name of the {@link Bundles}
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)
66 protected Bundle(Class
<E
> type
, Enum
<?
> name
,
67 TransBundle
<E
> descriptionBundle
) {
70 this.descriptionBundle
= descriptionBundle
;
72 this.map
= new HashMap
<String
, String
>();
73 this.changeMap
= new HashMap
<String
, String
>();
74 setBundle(name
, Locale
.getDefault(), false);
78 * Check if the setting is set into this {@link Bundle}.
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
86 * @return TRUE if the setting is set
88 public boolean isSet(E id
, boolean includeDefaultValue
) {
89 return isSet(id
.name(), includeDefaultValue
);
93 * Check if the setting is set into this {@link Bundle}.
96 * the id of the setting to check
97 * @param includeDefaultValue
98 * TRUE to only return false when the setting is explicitly set
99 * to NULL (and not just "no set") in the change maps
101 * @return TRUE if the setting is set
103 protected boolean isSet(String name
, boolean includeDefaultValue
) {
104 if (getString(name
, null) == null) {
105 if (!includeDefaultValue
|| getString(name
, "") == null) {
114 * Return the value associated to the given id as a {@link String}.
117 * the id of the value to get
119 * @return the associated value, or NULL if not found (not present in the
122 public String
getString(E id
) {
123 return getString(id
, null);
127 * Return the value associated to the given id as a {@link String}.
129 * If no value is associated, take the default one if any.
132 * the id of the value to get
134 * the default value when it is not present in the config file
136 * @return the associated value, or NULL if not found (not present in the
139 public String
getString(E id
, String def
) {
140 return getString(id
, def
, -1);
144 * Return the value associated to the given id as a {@link String}.
146 * If no value is associated (or if it is empty!), take the default one if
150 * the id of the value to get
152 * the default value when it is not present in the config file
154 * the item number to get for an array of values, or -1 for
157 * @return the associated value, or NULL if not found (not present in the
160 public String
getString(E id
, String def
, int item
) {
161 String rep
= getString(id
.name(), null);
163 rep
= getMetaDef(id
.name());
166 if (rep
== null || rep
.isEmpty()) {
171 List
<String
> values
= BundleHelper
.parseList(rep
, item
);
172 if (values
!= null && item
< values
.size()) {
173 return values
.get(item
);
183 * Set the value associated to the given id as a {@link String}.
186 * the id of the value to set
191 public void setString(E id
, String value
) {
192 setString(id
.name(), value
);
196 * Set the value associated to the given id as a {@link String}.
199 * the id of the value to set
203 * the item number to get for an array of values, or -1 for
207 public void setString(E id
, String value
, int item
) {
209 setString(id
.name(), value
);
211 List
<String
> values
= getList(id
);
212 setString(id
.name(), BundleHelper
.fromList(values
, value
, item
));
217 * Return the value associated to the given id as a {@link String} suffixed
218 * with the runtime value "_suffix" (that is, "_" and suffix).
220 * Will only accept suffixes that form an existing id.
222 * If no value is associated, take the default one if any.
225 * the id of the value to get
229 * @return the associated value, or NULL if not found (not present in the
232 public String
getStringX(E id
, String suffix
) {
233 return getStringX(id
, suffix
, null, -1);
237 * Return the value associated to the given id as a {@link String} suffixed
238 * with the runtime value "_suffix" (that is, "_" and suffix).
240 * Will only accept suffixes that form an existing id.
242 * If no value is associated, take the default one if any.
245 * the id of the value to get
249 * the default value when it is not present in the config file
251 * @return the associated value, or NULL if not found (not present in the
254 public String
getStringX(E id
, String suffix
, String def
) {
255 return getStringX(id
, suffix
, def
, -1);
259 * Return the value associated to the given id as a {@link String} suffixed
260 * with the runtime value "_suffix" (that is, "_" and suffix).
262 * Will only accept suffixes that form an existing id.
264 * If no value is associated, take the default one if any.
267 * the id of the value to get
271 * the default value when it is not present in the config file
273 * the item number to get for an array of values, or -1 for
276 * @return the associated value, or NULL if not found (not present in the
279 public String
getStringX(E id
, String suffix
, String def
, int item
) {
280 String key
= id
.name()
281 + (suffix
== null ?
"" : "_" + suffix
.toUpperCase());
284 id
= Enum
.valueOf(type
, key
);
285 return getString(id
, def
, item
);
286 } catch (IllegalArgumentException e
) {
293 * Set the value associated to the given id as a {@link String} suffixed
294 * with the runtime value "_suffix" (that is, "_" and suffix).
296 * Will only accept suffixes that form an existing id.
299 * the id of the value to set
305 public void setStringX(E id
, String suffix
, String value
) {
306 setStringX(id
, suffix
, value
, -1);
310 * Set the value associated to the given id as a {@link String} suffixed
311 * with the runtime value "_suffix" (that is, "_" and suffix).
313 * Will only accept suffixes that form an existing id.
316 * the id of the value to set
322 * the item number to get for an array of values, or -1 for
325 public void setStringX(E id
, String suffix
, String value
, int item
) {
326 String key
= id
.name()
327 + (suffix
== null ?
"" : "_" + suffix
.toUpperCase());
330 id
= Enum
.valueOf(type
, key
);
331 setString(id
, value
, item
);
332 } catch (IllegalArgumentException e
) {
337 * Return the value associated to the given id as a {@link Boolean}.
339 * If no value is associated, take the default one if any.
342 * the id of the value to get
344 * @return the associated value
346 public Boolean
getBoolean(E id
) {
347 return BundleHelper
.parseBoolean(getString(id
), -1);
351 * Return the value associated to the given id as a {@link Boolean}.
353 * If no value is associated, take the default one if any.
356 * the id of the value to get
358 * the default value when it is not present in the config file or
359 * if it is not a boolean value
361 * @return the associated value
363 public boolean getBoolean(E id
, boolean def
) {
364 Boolean value
= getBoolean(id
);
373 * Return the value associated to the given id as a {@link Boolean}.
375 * If no value is associated, take the default one if any.
378 * the id of the value to get
380 * the default value when it is not present in the config file or
381 * if it is not a boolean value
383 * the item number to get for an array of values, or -1 for
386 * @return the associated value
388 public Boolean
getBoolean(E id
, boolean def
, int item
) {
389 String value
= getString(id
);
391 return BundleHelper
.parseBoolean(value
, item
);
398 * Set the value associated to the given id as a {@link Boolean}.
401 * the id of the value to set
406 public void setBoolean(E id
, boolean value
) {
407 setBoolean(id
, value
, -1);
411 * Set the value associated to the given id as a {@link Boolean}.
414 * the id of the value to set
418 * the item number to get for an array of values, or -1 for
422 public void setBoolean(E id
, boolean value
, int item
) {
423 setString(id
, BundleHelper
.fromBoolean(value
), item
);
427 * Return the value associated to the given id as an {@link Integer}.
429 * If no value is associated, take the default one if any.
432 * the id of the value to get
434 * @return the associated value
436 public Integer
getInteger(E id
) {
437 String value
= getString(id
);
439 return BundleHelper
.parseInteger(value
, -1);
446 * Return the value associated to the given id as an int.
448 * If no value is associated, take the default one if any.
451 * the id of the value to get
453 * the default value when it is not present in the config file or
454 * if it is not a int value
456 * @return the associated value
458 public int getInteger(E id
, int def
) {
459 Integer value
= getInteger(id
);
468 * Return the value associated to the given id as an int.
470 * If no value is associated, take the default one if any.
473 * the id of the value to get
475 * the default value when it is not present in the config file or
476 * if it is not a int value
478 * the item number to get for an array of values, or -1 for
481 * @return the associated value
483 public Integer
getInteger(E id
, int def
, int item
) {
484 String value
= getString(id
);
486 return BundleHelper
.parseInteger(value
, item
);
493 * Set the value associated to the given id as a {@link Integer}.
496 * the id of the value to set
501 public void setInteger(E id
, int value
) {
502 setInteger(id
, value
, -1);
506 * Set the value associated to the given id as a {@link Integer}.
509 * the id of the value to set
513 * the item number to get for an array of values, or -1 for
517 public void setInteger(E id
, int value
, int item
) {
518 setString(id
, BundleHelper
.fromInteger(value
), item
);
522 * Return the value associated to the given id as a {@link Character}.
524 * If no value is associated, take the default one if any.
527 * the id of the value to get
529 * @return the associated value
531 public Character
getCharacter(E id
) {
532 return BundleHelper
.parseCharacter(getString(id
), -1);
536 * Return the value associated to the given id as a {@link Character}.
538 * If no value is associated, take the default one if any.
541 * the id of the value to get
543 * the default value when it is not present in the config file or
544 * if it is not a char value
546 * @return the associated value
548 public char getCharacter(E id
, char def
) {
549 Character value
= getCharacter(id
);
558 * Return the value associated to the given id as a {@link Character}.
560 * If no value is associated, take the default one if any.
563 * the id of the value to get
565 * the default value when it is not present in the config file or
566 * if it is not a char value
568 * the item number to get for an array of values, or -1 for
571 * @return the associated value
573 public Character
getCharacter(E id
, char def
, int item
) {
574 String value
= getString(id
);
576 return BundleHelper
.parseCharacter(value
, item
);
583 * Set the value associated to the given id as a {@link Character}.
586 * the id of the value to set
591 public void setCharacter(E id
, char value
) {
592 setCharacter(id
, value
, -1);
596 * Set the value associated to the given id as a {@link Character}.
599 * the id of the value to set
603 * the item number to get for an array of values, or -1 for
607 public void setCharacter(E id
, char value
, int item
) {
608 setString(id
, BundleHelper
.fromCharacter(value
), item
);
612 * Return the value associated to the given id as a colour if it is found
615 * The returned value is an ARGB value.
617 * If no value is associated, take the default one if any.
620 * the id of the value to get
622 * @return the associated value
624 public Integer
getColor(E id
) {
625 return BundleHelper
.parseColor(getString(id
), -1);
629 * Return the value associated to the given id as a colour if it is found
632 * The returned value is an ARGB value.
634 * If no value is associated, take the default one if any.
637 * the id of the value to get
639 * the default value when it is not present in the config file or
640 * if it is not a char value
642 * @return the associated value
644 public int getColor(E id
, int def
) {
645 Integer value
= getColor(id
);
654 * Return the value associated to the given id as a colour if it is found
657 * The returned value is an ARGB value.
659 * If no value is associated, take the default one if any.
662 * the id of the value to get
664 * the default value when it is not present in the config file or
665 * if it is not a char value
667 * the item number to get for an array of values, or -1 for
670 * @return the associated value
672 public Integer
getColor(E id
, int def
, int item
) {
673 String value
= getString(id
);
675 return BundleHelper
.parseColor(value
, item
);
682 * Set the value associated to the given id as a colour.
684 * The value is a BGRA value.
687 * the id of the value to set
691 public void setColor(E id
, Integer color
) {
692 setColor(id
, color
, -1);
696 * Set the value associated to the given id as a Color.
699 * the id of the value to set
703 * the item number to get for an array of values, or -1 for
707 public void setColor(E id
, int value
, int item
) {
708 setString(id
, BundleHelper
.fromColor(value
), item
);
712 * Return the value associated to the given id as a list of values if it is
713 * found and can be parsed.
715 * If no value is associated, take the default one if any.
718 * the id of the value to get
720 * @return the associated list, empty if the value is empty, NULL if it is
721 * not found or cannot be parsed as a list
723 public List
<String
> getList(E id
) {
724 return BundleHelper
.parseList(getString(id
), -1);
728 * Return the value associated to the given id as a list of values if it is
729 * found and can be parsed.
731 * If no value is associated, take the default one if any.
734 * the id of the value to get
736 * the default value when it is not present in the config file or
737 * if it is not a char value
739 * @return the associated list, empty if the value is empty, NULL if it is
740 * not found or cannot be parsed as a list
742 public List
<String
> getList(E id
, List
<String
> def
) {
743 List
<String
> value
= getList(id
);
752 * Return the value associated to the given id as a list of values if it is
753 * found and can be parsed.
755 * If no value is associated, take the default one if any.
758 * the id of the value to get
760 * the default value when it is not present in the config file or
761 * if it is not a char value
763 * the item number to get for an array of values, or -1 for
766 * @return the associated list, empty if the value is empty, NULL if it is
767 * not found or cannot be parsed as a list
769 public List
<String
> getList(E id
, List
<String
> def
, int item
) {
770 String value
= getString(id
);
772 return BundleHelper
.parseList(value
, item
);
779 * Set the value associated to the given id as a list of values.
782 * the id of the value to set
784 * the new list of values
786 public void setList(E id
, List
<String
> list
) {
787 setList(id
, list
, -1);
791 * Set the value associated to the given id as a {@link List}.
794 * the id of the value to set
798 * the item number to get for an array of values, or -1 for
802 public void setList(E id
, List
<String
> value
, int item
) {
803 setString(id
, BundleHelper
.fromList(value
), item
);
807 * Create/update the .properties file.
809 * Will use the most likely candidate as base if the file does not already
810 * exists and this resource is translatable (for instance, "en_US" will use
811 * "en" as a base if the resource is a translation file).
813 * Will update the files in {@link Bundles#getDirectory()}; it <b>MUST</b>
816 * @throws IOException
817 * in case of IO errors
819 public void updateFile() throws IOException
{
820 updateFile(Bundles
.getDirectory());
824 * Create/update the .properties file.
826 * Will use the most likely candidate as base if the file does not already
827 * exists and this resource is translatable (for instance, "en_US" will use
828 * "en" as a base if the resource is a translation file).
831 * the path where the .properties files are, <b>MUST NOT</b> be
834 * @throws IOException
835 * in case of IO errors
837 public void updateFile(String path
) throws IOException
{
838 File file
= getUpdateFile(path
);
840 BufferedWriter writer
= new BufferedWriter(new OutputStreamWriter(
841 new FileOutputStream(file
), "UTF-8"));
847 for (Field field
: type
.getDeclaredFields()) {
848 Meta meta
= field
.getAnnotation(Meta
.class);
850 E id
= Enum
.valueOf(type
, field
.getName());
851 String info
= getMetaInfo(meta
);
858 writeValue(writer
, id
);
866 * Delete the .properties file.
868 * Will use the most likely candidate as base if the file does not already
869 * exists and this resource is translatable (for instance, "en_US" will use
870 * "en" as a base if the resource is a translation file).
872 * Will delete the files in {@link Bundles#getDirectory()}; it <b>MUST</b>
875 * @return TRUE if the file was deleted
877 public boolean deleteFile() {
878 return deleteFile(Bundles
.getDirectory());
882 * Delete the .properties file.
884 * Will use the most likely candidate as base if the file does not already
885 * exists and this resource is translatable (for instance, "en_US" will use
886 * "en" as a base if the resource is a translation file).
889 * the path where the .properties files are, <b>MUST NOT</b> be
892 * @return TRUE if the file was deleted
894 public boolean deleteFile(String path
) {
895 File file
= getUpdateFile(path
);
896 return file
.delete();
900 * The description {@link TransBundle}, that is, a {@link TransBundle}
901 * dedicated to the description of the values of the given {@link Bundle}
904 * @return the description {@link TransBundle}
906 public TransBundle
<E
> getDescriptionBundle() {
907 return descriptionBundle
;
911 * Reload the {@link Bundle} data files.
913 * @param resetToDefault
914 * reset to the default configuration (do not look into the
915 * possible user configuration files, only take the original
918 public void reload(boolean resetToDefault
) {
919 setBundle(keyType
, Locale
.getDefault(), resetToDefault
);
923 * Check if the internal map contains the given key.
926 * the key to check for
928 * @return true if it does
930 protected boolean containsKey(String key
) {
931 return changeMap
.containsKey(key
) || map
.containsKey(key
);
935 * The default {@link MetaInfo.def} value for the given enumeration name.
938 * the enumeration name (the "id")
940 * @return the def value in the {@link MetaInfo} or "" if none (never NULL)
942 protected String
getMetaDef(String id
) {
945 Meta meta
= type
.getDeclaredField(id
).getAnnotation(Meta
.class);
947 } catch (NoSuchFieldException e
) {
948 } catch (SecurityException e
) {
959 * Get the value for the given key if it exists in the internal map, or
960 * <tt>def</tt> if not.
962 * DO NOT get the default meta value (MetaInfo.def()).
965 * the key to check for
967 * the default value when it is not present in the internal map
969 * @return the value, or <tt>def</tt> if not found
971 protected String
getString(String key
, String def
) {
972 if (changeMap
.containsKey(key
)) {
973 return changeMap
.get(key
);
976 if (map
.containsKey(key
)) {
984 * Set the value for this key, in the change map (it is kept in memory, not
990 * the associated value
992 protected void setString(String key
, String value
) {
993 changeMap
.put(key
, value
== null ?
null : value
.trim());
997 * Return formated, display-able information from the {@link Meta} field
998 * given. Each line will always starts with a "#" character.
1001 * the {@link Meta} field
1003 * @return the information to display or NULL if none
1005 protected String
getMetaInfo(Meta meta
) {
1006 String desc
= meta
.description();
1007 boolean group
= meta
.group();
1008 Meta
.Format format
= meta
.format();
1009 String
[] list
= meta
.list();
1010 boolean nullable
= meta
.nullable();
1011 String def
= meta
.def();
1012 boolean array
= meta
.array();
1014 // Default, empty values -> NULL
1015 if (desc
.length() + list
.length
+ def
.length() == 0 && !group
1016 && nullable
&& format
== Format
.STRING
) {
1020 StringBuilder builder
= new StringBuilder();
1021 for (String line
: desc
.split("\n")) {
1022 builder
.append("# ").append(line
).append("\n");
1026 builder
.append("# This item is used as a group, its content is not expected to be used.");
1028 builder
.append("# (FORMAT: ").append(format
)
1029 .append(nullable ?
"" : ", required");
1030 builder
.append(") ");
1032 if (list
.length
> 0) {
1033 builder
.append("\n# ALLOWED VALUES: ");
1034 boolean first
= true;
1035 for (String value
: list
) {
1037 builder
.append(", ");
1039 builder
.append(BundleHelper
.escape(value
));
1045 builder
.append("\n# (This item accepts a list of ^escaped comma-separated values)");
1049 return builder
.toString();
1053 * The display name used in the <tt>.properties file</tt>.
1057 protected String
getBundleDisplayName() {
1058 return keyType
.toString();
1062 * Write the header found in the configuration <tt>.properties</tt> file of
1063 * this {@link Bundles}.
1066 * the {@link Writer} to write the header in
1068 * @throws IOException
1069 * in case of IO error
1071 protected void writeHeader(Writer writer
) throws IOException
{
1072 writer
.write("# " + getBundleDisplayName() + "\n");
1073 writer
.write("#\n");
1077 * Write the given data to the config file, i.e., "MY_ID = my_curent_value"
1078 * followed by a new line.
1080 * Will prepend a # sign if the is is not set (see
1081 * {@link Bundle#isSet(Enum, boolean)}).
1084 * the {@link Writer} to write into
1088 * @throws IOException
1089 * in case of IO error
1091 protected void writeValue(Writer writer
, E id
) throws IOException
{
1092 boolean set
= isSet(id
, false);
1093 writeValue(writer
, id
.name(), getString(id
), set
);
1097 * Write the given data to the config file, i.e., "MY_ID = my_curent_value"
1098 * followed by a new line.
1100 * Will prepend a # sign if the is is not set.
1103 * the {@link Writer} to write into
1109 * the value is set in this {@link Bundle}
1111 * @throws IOException
1112 * in case of IO error
1114 protected void writeValue(Writer writer
, String id
, String value
,
1115 boolean set
) throws IOException
{
1122 writer
.write(" = ");
1124 if (value
== null) {
1128 String
[] lines
= value
.replaceAll("\t", "\\\\\\t").split("\n");
1129 for (int i
= 0; i
< lines
.length
; i
++) {
1130 writer
.write(lines
[i
]);
1131 if (i
< lines
.length
- 1) {
1132 writer
.write("\\n\\");
1139 * Return the source file for this {@link Bundles} from the given path.
1142 * the path where the .properties files are
1144 * @return the source {@link File}
1146 protected File
getUpdateFile(String path
) {
1147 return new File(path
, keyType
.name() + ".properties");
1151 * Change the currently used bundle, and reset all changes.
1154 * the name of the bundle to load
1156 * the {@link Locale} to use
1157 * @param resetToDefault
1158 * reset to the default configuration (do not look into the
1159 * possible user configuration files, only take the original
1162 protected void setBundle(Enum
<?
> name
, Locale locale
, boolean resetToDefault
) {
1164 String dir
= Bundles
.getDirectory();
1165 String bname
= type
.getPackage().getName() + "." + name
.name();
1167 boolean found
= false;
1168 if (!resetToDefault
&& dir
!= null) {
1169 // Look into Bundles.getDirectory() for .properties files
1171 File file
= getPropertyFile(dir
, name
.name(), locale
);
1173 Reader reader
= new InputStreamReader(new FileInputStream(
1175 resetMap(new PropertyResourceBundle(reader
));
1178 } catch (IOException e
) {
1179 e
.printStackTrace();
1184 // Look into the package itself for resources
1186 resetMap(ResourceBundle
1187 .getBundle(bname
, locale
, type
.getClassLoader(),
1188 new FixedResourceBundleControl()));
1190 } catch (MissingResourceException e
) {
1191 } catch (Exception e
) {
1192 e
.printStackTrace();
1197 // We have no bundle for this Bundle
1198 System
.err
.println("No bundle found for: " + bname
);
1204 * Reset the backing map to the content of the given bundle, or with NULL
1205 * values if bundle is NULL.
1208 * the bundle to copy
1210 protected void resetMap(ResourceBundle bundle
) {
1212 for (Field field
: type
.getDeclaredFields()) {
1214 Meta meta
= field
.getAnnotation(Meta
.class);
1216 E id
= Enum
.valueOf(type
, field
.getName());
1219 if (bundle
!= null) {
1220 value
= bundle
.getString(id
.name());
1225 this.map
.put(id
.name(), value
== null ?
null : value
.trim());
1227 } catch (MissingResourceException e
) {
1233 * Take a snapshot of the changes in memory in this {@link Bundle} made by
1234 * the "set" methods ( {@link Bundle#setString(Enum, String)}...) at the
1237 * @return a snapshot to use with {@link Bundle#restoreSnapshot(Object)}
1239 public Object
takeSnapshot() {
1240 return new HashMap
<String
, String
>(changeMap
);
1244 * Restore a snapshot taken with {@link Bundle}, or reset the current
1245 * changes if the snapshot is NULL.
1248 * the snapshot or NULL
1250 @SuppressWarnings("unchecked")
1251 public void restoreSnapshot(Object snap
) {
1255 if (snap
instanceof Map
) {
1256 changeMap
= (Map
<String
, String
>) snap
;
1258 throw new RuntimeException(
1259 "Restoring changes in a Bundle must be done on a changes snapshot, "
1260 + "or NULL to discard current changes");
1266 * Return the resource file that is closer to the {@link Locale}.
1269 * the directory to look into
1271 * the file base name (without <tt>.properties</tt>)
1273 * the {@link Locale}
1275 * @return the closest match or NULL if none
1277 private File
getPropertyFile(String dir
, String name
, Locale locale
) {
1278 List
<String
> locales
= new ArrayList
<String
>();
1279 if (locale
!= null) {
1280 String country
= locale
.getCountry() == null ?
"" : locale
1282 String language
= locale
.getLanguage() == null ?
"" : locale
1284 if (!language
.isEmpty() && !country
.isEmpty()) {
1285 locales
.add("_" + language
+ "-" + country
);
1287 if (!language
.isEmpty()) {
1288 locales
.add("_" + language
);
1295 for (String loc
: locales
) {
1296 file
= new File(dir
, name
+ loc
+ ".properties");
1297 if (file
.exists()) {