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
.InputStream
;
9 import java
.io
.InputStreamReader
;
10 import java
.io
.OutputStreamWriter
;
11 import java
.io
.Reader
;
12 import java
.io
.Writer
;
13 import java
.lang
.reflect
.Field
;
14 import java
.util
.ArrayList
;
15 import java
.util
.HashMap
;
16 import java
.util
.List
;
17 import java
.util
.Locale
;
19 import java
.util
.MissingResourceException
;
20 import java
.util
.PropertyResourceBundle
;
21 import java
.util
.ResourceBundle
;
23 import be
.nikiroo
.utils
.resources
.Meta
.Format
;
26 * This class encapsulate a {@link ResourceBundle} in UTF-8. It allows to
27 * retrieve values associated to an enumeration, and allows some additional
30 * It also sports a writable change map, and you can save back the
31 * {@link Bundle} to file with {@link Bundle#updateFile(String)}.
34 * the enum to use to get values out of this class
39 public class Bundle
<E
extends Enum
<E
>> {
41 protected Class
<E
> type
;
43 * The {@link Enum} associated to this {@link Bundle} (all the keys used in
44 * this {@link Bundle} will be of this type).
46 protected Enum
<?
> keyType
;
48 private TransBundle
<E
> descriptionBundle
;
51 private Map
<String
, String
> map
;
53 private Map
<String
, String
> changeMap
;
56 * Create a new {@link Bundles} of the given name.
59 * a runtime instance of the class of E
61 * the name of the {@link Bundles}
62 * @param descriptionBundle
63 * the description {@link TransBundle}, that is, a
64 * {@link TransBundle} dedicated to the description of the values
65 * of the given {@link Bundle} (can be NULL)
67 protected Bundle(Class
<E
> type
, Enum
<?
> name
,
68 TransBundle
<E
> descriptionBundle
) {
71 this.descriptionBundle
= descriptionBundle
;
73 this.map
= new HashMap
<String
, String
>();
74 this.changeMap
= new HashMap
<String
, String
>();
75 setBundle(name
, Locale
.getDefault(), false);
79 * Check if the setting is set into this {@link Bundle}.
82 * the id of the setting to check
83 * @param includeDefaultValue
84 * TRUE to only return false when the setting is not set AND
85 * there is no default value
87 * @return TRUE if the setting is set
89 public boolean isSet(E id
, boolean includeDefaultValue
) {
90 return isSet(id
.name(), includeDefaultValue
);
94 * Check if the setting is set into this {@link Bundle}.
97 * the id of the setting to check
98 * @param includeDefaultValue
99 * TRUE to only return false when the setting is explicitly set
100 * to NULL (and not just "no set") in the change maps
102 * @return TRUE if the setting is set
104 protected boolean isSet(String name
, boolean includeDefaultValue
) {
105 if (getString(name
, null) == null) {
106 if (!includeDefaultValue
|| getString(name
, "") == null) {
115 * Return the value associated to the given id as a {@link String}.
118 * the id of the value to get
120 * @return the associated value, or NULL if not found (not present in the
123 public String
getString(E id
) {
124 return getString(id
, null);
128 * Return the value associated to the given id as a {@link String}.
130 * If no value is associated, take the default one if any.
133 * the id of the value to get
135 * the default value when it is not present in the config file
137 * @return the associated value, or <tt>def</tt> if not found (not present
138 * in the resource file)
140 public String
getString(E id
, String def
) {
141 return getString(id
, def
, -1);
145 * Return the value associated to the given id as a {@link String}.
147 * If no value is associated (or if it is empty!), take the default one if
151 * the id of the value to get
153 * the default value when it is not present in the config file
155 * the item number to get for an array of values, or -1 for
158 * @return the associated value, <tt>def</tt> if not found (not present in
159 * the resource file) or NULL if the item is specified (not -1) and
162 public String
getString(E id
, String def
, int item
) {
163 String rep
= getString(id
.name(), null);
165 rep
= getMetaDef(id
.name());
173 List
<String
> values
= BundleHelper
.parseList(rep
, item
);
174 if (values
!= null && item
< values
.size()) {
175 return values
.get(item
);
185 * Set the value associated to the given id as a {@link String}.
188 * the id of the value to set
193 public void setString(E id
, String value
) {
194 setString(id
.name(), value
);
198 * Set the value associated to the given id as a {@link String}.
201 * the id of the value to set
205 * the item number to get for an array of values, or -1 for
209 public void setString(E id
, String value
, int item
) {
211 setString(id
.name(), value
);
213 List
<String
> values
= getList(id
);
214 setString(id
.name(), BundleHelper
.fromList(values
, value
, item
));
219 * Return the value associated to the given id as a {@link String} suffixed
220 * with the runtime value "_suffix" (that is, "_" and suffix).
222 * Will only accept suffixes that form an existing id.
224 * If no value is associated, take the default one if any.
227 * the id of the value to get
231 * @return the associated value, or NULL if not found (not present in the
234 public String
getStringX(E id
, String suffix
) {
235 return getStringX(id
, suffix
, null, -1);
239 * Return the value associated to the given id as a {@link String} suffixed
240 * with the runtime value "_suffix" (that is, "_" and suffix).
242 * Will only accept suffixes that form an existing id.
244 * If no value is associated, take the default one if any.
247 * the id of the value to get
251 * the default value when it is not present in the config file
253 * @return the associated value, or NULL if not found (not present in the
256 public String
getStringX(E id
, String suffix
, String def
) {
257 return getStringX(id
, suffix
, def
, -1);
261 * Return the value associated to the given id as a {@link String} suffixed
262 * with the runtime value "_suffix" (that is, "_" and suffix).
264 * Will only accept suffixes that form an existing id.
266 * If no value is associated, take the default one if any.
269 * the id of the value to get
273 * the default value when it is not present in the config file
275 * the item number to get for an array of values, or -1 for
278 * @return the associated value, <tt>def</tt> if not found (not present in
279 * the resource file), NULL if the item is specified (not -1) but
280 * does not exist and NULL if bad key
282 public String
getStringX(E id
, String suffix
, String def
, int item
) {
283 String key
= id
.name()
284 + (suffix
== null ?
"" : "_" + suffix
.toUpperCase());
287 id
= Enum
.valueOf(type
, key
);
288 return getString(id
, def
, item
);
289 } catch (IllegalArgumentException e
) {
296 * Set the value associated to the given id as a {@link String} suffixed
297 * with the runtime value "_suffix" (that is, "_" and suffix).
299 * Will only accept suffixes that form an existing id.
302 * the id of the value to set
308 public void setStringX(E id
, String suffix
, String value
) {
309 setStringX(id
, suffix
, value
, -1);
313 * Set the value associated to the given id as a {@link String} suffixed
314 * with the runtime value "_suffix" (that is, "_" and suffix).
316 * Will only accept suffixes that form an existing id.
319 * the id of the value to set
325 * the item number to get for an array of values, or -1 for
328 public void setStringX(E id
, String suffix
, String value
, int item
) {
329 String key
= id
.name()
330 + (suffix
== null ?
"" : "_" + suffix
.toUpperCase());
333 id
= Enum
.valueOf(type
, key
);
334 setString(id
, value
, item
);
335 } catch (IllegalArgumentException e
) {
340 * Return the value associated to the given id as a {@link Boolean}.
342 * If no value is associated, take the default one if any.
345 * the id of the value to get
347 * @return the associated value
349 public Boolean
getBoolean(E id
) {
350 return BundleHelper
.parseBoolean(getString(id
), -1);
354 * Return the value associated to the given id as a {@link Boolean}.
356 * If no value is associated, take the default one if any.
359 * the id of the value to get
361 * the default value when it is not present in the config file or
362 * if it is not a boolean value
364 * @return the associated value
366 public boolean getBoolean(E id
, boolean def
) {
367 Boolean value
= getBoolean(id
);
376 * Return the value associated to the given id as a {@link Boolean}.
378 * If no value is associated, take the default one if any.
381 * the id of the value to get
383 * the default value when it is not present in the config file or
384 * if it is not a boolean value
386 * the item number to get for an array of values, or -1 for
389 * @return the associated value
391 public Boolean
getBoolean(E id
, boolean def
, int item
) {
392 String value
= getString(id
);
394 return BundleHelper
.parseBoolean(value
, item
);
401 * Set the value associated to the given id as a {@link Boolean}.
404 * the id of the value to set
409 public void setBoolean(E id
, boolean value
) {
410 setBoolean(id
, value
, -1);
414 * Set the value associated to the given id as a {@link Boolean}.
417 * the id of the value to set
421 * the item number to get for an array of values, or -1 for
425 public void setBoolean(E id
, boolean value
, int item
) {
426 setString(id
, BundleHelper
.fromBoolean(value
), item
);
430 * Return the value associated to the given id as an {@link Integer}.
432 * If no value is associated, take the default one if any.
435 * the id of the value to get
437 * @return the associated value
439 public Integer
getInteger(E id
) {
440 String value
= getString(id
);
442 return BundleHelper
.parseInteger(value
, -1);
449 * Return the value associated to the given id as an int.
451 * If no value is associated, take the default one if any.
454 * the id of the value to get
456 * the default value when it is not present in the config file or
457 * if it is not a int value
459 * @return the associated value
461 public int getInteger(E id
, int def
) {
462 Integer value
= getInteger(id
);
471 * Return the value associated to the given id as an int.
473 * If no value is associated, take the default one if any.
476 * the id of the value to get
478 * the default value when it is not present in the config file or
479 * if it is not a int value
481 * the item number to get for an array of values, or -1 for
484 * @return the associated value
486 public Integer
getInteger(E id
, int def
, int item
) {
487 String value
= getString(id
);
489 return BundleHelper
.parseInteger(value
, item
);
496 * Set the value associated to the given id as a {@link Integer}.
499 * the id of the value to set
504 public void setInteger(E id
, int value
) {
505 setInteger(id
, value
, -1);
509 * Set the value associated to the given id as a {@link Integer}.
512 * the id of the value to set
516 * the item number to get for an array of values, or -1 for
520 public void setInteger(E id
, int value
, int item
) {
521 setString(id
, BundleHelper
.fromInteger(value
), item
);
525 * Return the value associated to the given id as a {@link Character}.
527 * If no value is associated, take the default one if any.
530 * the id of the value to get
532 * @return the associated value
534 public Character
getCharacter(E id
) {
535 return BundleHelper
.parseCharacter(getString(id
), -1);
539 * Return the value associated to the given id as a {@link Character}.
541 * If no value is associated, take the default one if any.
544 * the id of the value to get
546 * the default value when it is not present in the config file or
547 * if it is not a char value
549 * @return the associated value
551 public char getCharacter(E id
, char def
) {
552 Character value
= getCharacter(id
);
561 * Return the value associated to the given id as a {@link Character}.
563 * If no value is associated, take the default one if any.
566 * the id of the value to get
568 * the default value when it is not present in the config file or
569 * if it is not a char value
571 * the item number to get for an array of values, or -1 for
574 * @return the associated value
576 public Character
getCharacter(E id
, char def
, int item
) {
577 String value
= getString(id
);
579 return BundleHelper
.parseCharacter(value
, item
);
586 * Set the value associated to the given id as a {@link Character}.
589 * the id of the value to set
594 public void setCharacter(E id
, char value
) {
595 setCharacter(id
, value
, -1);
599 * Set the value associated to the given id as a {@link Character}.
602 * the id of the value to set
606 * the item number to get for an array of values, or -1 for
610 public void setCharacter(E id
, char value
, int item
) {
611 setString(id
, BundleHelper
.fromCharacter(value
), item
);
615 * Return the value associated to the given id as a colour if it is found
618 * The returned value is an ARGB value.
620 * If no value is associated, take the default one if any.
623 * the id of the value to get
625 * @return the associated value
627 public Integer
getColor(E id
) {
628 return BundleHelper
.parseColor(getString(id
), -1);
632 * Return the value associated to the given id as a colour if it is found
635 * The returned value is an ARGB value.
637 * If no value is associated, take the default one if any.
640 * the id of the value to get
642 * the default value when it is not present in the config file or
643 * if it is not a char value
645 * @return the associated value
647 public int getColor(E id
, int def
) {
648 Integer value
= getColor(id
);
657 * Return the value associated to the given id as a colour if it is found
660 * The returned value is an ARGB value.
662 * If no value is associated, take the default one if any.
665 * the id of the value to get
667 * the default value when it is not present in the config file or
668 * if it is not a char value
670 * the item number to get for an array of values, or -1 for
673 * @return the associated value
675 public Integer
getColor(E id
, int def
, int item
) {
676 String value
= getString(id
);
678 return BundleHelper
.parseColor(value
, item
);
685 * Set the value associated to the given id as a colour.
687 * The value is a BGRA value.
690 * the id of the value to set
694 public void setColor(E id
, Integer color
) {
695 setColor(id
, color
, -1);
699 * Set the value associated to the given id as a Color.
702 * the id of the value to set
706 * the item number to get for an array of values, or -1 for
710 public void setColor(E id
, int value
, int item
) {
711 setString(id
, BundleHelper
.fromColor(value
), item
);
715 * Return the value associated to the given id as a list of values if it is
716 * found and can be parsed.
718 * If no value is associated, take the default one if any.
721 * the id of the value to get
723 * @return the associated list, empty if the value is empty, NULL if it is
724 * not found or cannot be parsed as a list
726 public List
<String
> getList(E id
) {
727 return BundleHelper
.parseList(getString(id
), -1);
731 * Return the value associated to the given id as a list of values if it is
732 * found and can be parsed.
734 * If no value is associated, take the default one if any.
737 * the id of the value to get
739 * the default value when it is not present in the config file or
740 * if it is not a char value
742 * @return the associated list, empty if the value is empty, NULL if it is
743 * not found or cannot be parsed as a list
745 public List
<String
> getList(E id
, List
<String
> def
) {
746 List
<String
> value
= getList(id
);
755 * Return the value associated to the given id as a list of values if it is
756 * found and can be parsed.
758 * If no value is associated, take the default one if any.
761 * the id of the value to get
763 * the default value when it is not present in the config file or
764 * if it is not a char value
766 * the item number to get for an array of values, or -1 for
769 * @return the associated list, empty if the value is empty, NULL if it is
770 * not found or cannot be parsed as a list
772 public List
<String
> getList(E id
, List
<String
> def
, int item
) {
773 String value
= getString(id
);
775 return BundleHelper
.parseList(value
, item
);
782 * Set the value associated to the given id as a list of values.
785 * the id of the value to set
787 * the new list of values
789 public void setList(E id
, List
<String
> list
) {
790 setList(id
, list
, -1);
794 * Set the value associated to the given id as a {@link List}.
797 * the id of the value to set
801 * the item number to get for an array of values, or -1 for
805 public void setList(E id
, List
<String
> value
, int item
) {
806 setString(id
, BundleHelper
.fromList(value
), item
);
810 * Create/update the .properties file.
812 * Will use the most likely candidate as base if the file does not already
813 * exists and this resource is translatable (for instance, "en_US" will use
814 * "en" as a base if the resource is a translation file).
816 * Will update the files in {@link Bundles#getDirectory()}; it <b>MUST</b>
819 * @throws IOException
820 * in case of IO errors
822 public void updateFile() throws IOException
{
823 updateFile(Bundles
.getDirectory());
827 * Create/update the .properties file.
829 * Will use the most likely candidate as base if the file does not already
830 * exists and this resource is translatable (for instance, "en_US" will use
831 * "en" as a base if the resource is a translation file).
834 * the path where the .properties files are, <b>MUST NOT</b> be
837 * @throws IOException
838 * in case of IO errors
840 public void updateFile(String path
) throws IOException
{
841 File file
= getUpdateFile(path
);
843 BufferedWriter writer
= new BufferedWriter(new OutputStreamWriter(
844 new FileOutputStream(file
), "UTF-8"));
850 for (Field field
: type
.getDeclaredFields()) {
851 Meta meta
= field
.getAnnotation(Meta
.class);
853 E id
= Enum
.valueOf(type
, field
.getName());
854 String info
= getMetaInfo(meta
);
861 writeValue(writer
, id
);
869 * Delete the .properties file.
871 * Will use the most likely candidate as base if the file does not already
872 * exists and this resource is translatable (for instance, "en_US" will use
873 * "en" as a base if the resource is a translation file).
875 * Will delete the files in {@link Bundles#getDirectory()}; it <b>MUST</b>
878 * @return TRUE if the file was deleted
880 public boolean deleteFile() {
881 return deleteFile(Bundles
.getDirectory());
885 * Delete the .properties file.
887 * Will use the most likely candidate as base if the file does not already
888 * exists and this resource is translatable (for instance, "en_US" will use
889 * "en" as a base if the resource is a translation file).
892 * the path where the .properties files are, <b>MUST NOT</b> be
895 * @return TRUE if the file was deleted
897 public boolean deleteFile(String path
) {
898 File file
= getUpdateFile(path
);
899 return file
.delete();
903 * The description {@link TransBundle}, that is, a {@link TransBundle}
904 * dedicated to the description of the values of the given {@link Bundle}
907 * @return the description {@link TransBundle}
909 public TransBundle
<E
> getDescriptionBundle() {
910 return descriptionBundle
;
914 * Reload the {@link Bundle} data files.
916 * @param resetToDefault
917 * reset to the default configuration (do not look into the
918 * possible user configuration files, only take the original
921 public void reload(boolean resetToDefault
) {
922 setBundle(keyType
, Locale
.getDefault(), resetToDefault
);
926 * Check if the internal map contains the given key.
929 * the key to check for
931 * @return true if it does
933 protected boolean containsKey(String key
) {
934 return changeMap
.containsKey(key
) || map
.containsKey(key
);
938 * The default {@link Meta#def()} value for the given enumeration name.
941 * the enumeration name (the "id")
943 * @return the def value in the {@link MetaInfo} or "" if none (never NULL)
945 protected String
getMetaDef(String id
) {
948 Meta meta
= type
.getDeclaredField(id
).getAnnotation(Meta
.class);
950 } catch (NoSuchFieldException e
) {
951 } catch (SecurityException e
) {
962 * Get the value for the given key if it exists in the internal map, or
963 * <tt>def</tt> if not.
965 * DO NOT get the default meta value (MetaInfo.def()).
968 * the key to check for
970 * the default value when it is not present in the internal map
972 * @return the value, or <tt>def</tt> if not found
974 protected String
getString(String key
, String def
) {
975 if (changeMap
.containsKey(key
)) {
976 return changeMap
.get(key
);
979 if (map
.containsKey(key
)) {
987 * Set the value for this key, in the change map (it is kept in memory, not
993 * the associated value
995 protected void setString(String key
, String value
) {
996 changeMap
.put(key
, value
== null ?
null : value
.trim());
1000 * Return formated, display-able information from the {@link Meta} field
1001 * given. Each line will always starts with a "#" character.
1004 * the {@link Meta} field
1006 * @return the information to display or NULL if none
1008 protected String
getMetaInfo(Meta meta
) {
1009 String desc
= meta
.description();
1010 boolean group
= meta
.group();
1011 Meta
.Format format
= meta
.format();
1012 String
[] list
= meta
.list();
1013 boolean nullable
= meta
.nullable();
1014 String def
= meta
.def();
1015 boolean array
= meta
.array();
1017 // Default, empty values -> NULL
1018 if (desc
.length() + list
.length
+ def
.length() == 0 && !group
1019 && nullable
&& format
== Format
.STRING
) {
1023 StringBuilder builder
= new StringBuilder();
1024 for (String line
: desc
.split("\n")) {
1025 builder
.append("# ").append(line
).append("\n");
1029 builder
.append("# This item is used as a group, its content is not expected to be used.");
1031 builder
.append("# (FORMAT: ").append(format
)
1032 .append(nullable ?
"" : ", required");
1033 builder
.append(") ");
1035 if (list
.length
> 0) {
1036 builder
.append("\n# ALLOWED VALUES: ");
1037 boolean first
= true;
1038 for (String value
: list
) {
1040 builder
.append(", ");
1042 builder
.append(BundleHelper
.escape(value
));
1048 builder
.append("\n# (This item accepts a list of ^escaped comma-separated values)");
1052 return builder
.toString();
1056 * The display name used in the <tt>.properties file</tt>.
1060 protected String
getBundleDisplayName() {
1061 return keyType
.toString();
1065 * Write the header found in the configuration <tt>.properties</tt> file of
1066 * this {@link Bundles}.
1069 * the {@link Writer} to write the header in
1071 * @throws IOException
1072 * in case of IO error
1074 protected void writeHeader(Writer writer
) throws IOException
{
1075 writer
.write("# " + getBundleDisplayName() + "\n");
1076 writer
.write("#\n");
1080 * Write the given data to the config file, i.e., "MY_ID = my_curent_value"
1081 * followed by a new line.
1083 * Will prepend a # sign if the is is not set (see
1084 * {@link Bundle#isSet(Enum, boolean)}).
1087 * the {@link Writer} to write into
1091 * @throws IOException
1092 * in case of IO error
1094 protected void writeValue(Writer writer
, E id
) throws IOException
{
1095 boolean set
= isSet(id
, false);
1096 writeValue(writer
, id
.name(), getString(id
), set
);
1100 * Write the given data to the config file, i.e., "MY_ID = my_curent_value"
1101 * followed by a new line.
1103 * Will prepend a # sign if the is is not set.
1106 * the {@link Writer} to write into
1112 * the value is set in this {@link Bundle}
1114 * @throws IOException
1115 * in case of IO error
1117 protected void writeValue(Writer writer
, String id
, String value
,
1118 boolean set
) throws IOException
{
1125 writer
.write(" = ");
1127 if (value
== null) {
1131 String
[] lines
= value
.replaceAll("\t", "\\\\\\t").split("\n");
1132 for (int i
= 0; i
< lines
.length
; i
++) {
1133 writer
.write(lines
[i
]);
1134 if (i
< lines
.length
- 1) {
1135 writer
.write("\\n\\");
1142 * Return the source file for this {@link Bundles} from the given path.
1145 * the path where the .properties files are
1147 * @return the source {@link File}
1149 protected File
getUpdateFile(String path
) {
1150 return new File(path
, keyType
.name() + ".properties");
1154 * Change the currently used bundle, and reset all changes.
1157 * the name of the bundle to load
1159 * the {@link Locale} to use
1160 * @param resetToDefault
1161 * reset to the default configuration (do not look into the
1162 * possible user configuration files, only take the original
1165 protected void setBundle(Enum
<?
> name
, Locale locale
, boolean resetToDefault
) {
1167 String dir
= Bundles
.getDirectory();
1168 String bname
= type
.getPackage().getName() + "." + name
.name();
1170 boolean found
= false;
1171 if (!resetToDefault
&& dir
!= null) {
1173 // Look into Bundles.getDirectory() for .properties files
1174 File file
= getPropertyFile(dir
, name
.name(), locale
);
1176 InputStream in
= new FileInputStream(file
);
1178 Reader reader
= new InputStreamReader(in
, "UTF-8");
1180 resetMap(new PropertyResourceBundle(reader
));
1189 } catch (IOException e
) {
1190 e
.printStackTrace();
1195 // Look into the package itself for resources
1197 resetMap(ResourceBundle
1198 .getBundle(bname
, locale
, type
.getClassLoader(),
1199 new FixedResourceBundleControl()));
1201 } catch (MissingResourceException e
) {
1202 } catch (Exception e
) {
1203 e
.printStackTrace();
1208 // We have no bundle for this Bundle
1209 System
.err
.println("No bundle found for: " + bname
);
1215 * Reset the backing map to the content of the given bundle, or with NULL
1216 * values if bundle is NULL.
1219 * the bundle to copy
1221 protected void resetMap(ResourceBundle bundle
) {
1223 if (bundle
!= null) {
1224 for (Field field
: type
.getDeclaredFields()) {
1226 Meta meta
= field
.getAnnotation(Meta
.class);
1228 E id
= Enum
.valueOf(type
, field
.getName());
1229 String value
= bundle
.getString(id
.name());
1230 this.map
.put(id
.name(),
1231 value
== null ?
null : value
.trim());
1233 } catch (MissingResourceException e
) {
1240 * Take a snapshot of the changes in memory in this {@link Bundle} made by
1241 * the "set" methods ( {@link Bundle#setString(Enum, String)}...) at the
1244 * @return a snapshot to use with {@link Bundle#restoreSnapshot(Object)}
1246 public Object
takeSnapshot() {
1247 return new HashMap
<String
, String
>(changeMap
);
1251 * Restore a snapshot taken with {@link Bundle}, or reset the current
1252 * changes if the snapshot is NULL.
1255 * the snapshot or NULL
1257 @SuppressWarnings("unchecked")
1258 public void restoreSnapshot(Object snap
) {
1262 if (snap
instanceof Map
) {
1263 changeMap
= (Map
<String
, String
>) snap
;
1265 throw new RuntimeException(
1266 "Restoring changes in a Bundle must be done on a changes snapshot, "
1267 + "or NULL to discard current changes");
1273 * Return the resource file that is closer to the {@link Locale}.
1276 * the directory to look into
1278 * the file base name (without <tt>.properties</tt>)
1280 * the {@link Locale}
1282 * @return the closest match or NULL if none
1284 private File
getPropertyFile(String dir
, String name
, Locale locale
) {
1285 List
<String
> locales
= new ArrayList
<String
>();
1286 if (locale
!= null) {
1287 String country
= locale
.getCountry() == null ?
"" : locale
1289 String language
= locale
.getLanguage() == null ?
"" : locale
1291 if (!language
.isEmpty() && !country
.isEmpty()) {
1292 locales
.add("_" + language
+ "-" + country
);
1294 if (!language
.isEmpty()) {
1295 locales
.add("_" + language
);
1302 for (String loc
: locales
) {
1303 file
= new File(dir
, name
+ loc
+ ".properties");
1304 if (file
.exists()) {