Merge branch 'subtree'
[nikiroo-utils.git] / src / be / nikiroo / utils / resources / Bundle.java
index 21021e065f05a938f4521bfb5fd399b40847a77d..fe3ac1a3be4eb2c66580f855ea9df1ee206085dc 100644 (file)
@@ -5,6 +5,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.io.Reader;
@@ -92,11 +93,11 @@ public class Bundle<E extends Enum<E>> {
        /**
         * Check if the setting is set into this {@link Bundle}.
         * 
-        * @param id
+        * @param name
         *            the id of the setting to check
         * @param includeDefaultValue
-        *            TRUE to only return false when the setting is not set AND
-        *            there is no default value
+        *            TRUE to only return false when the setting is explicitly set
+        *            to NULL (and not just "no set") in the change maps
         * 
         * @return TRUE if the setting is set
         */
@@ -133,8 +134,8 @@ public class Bundle<E extends Enum<E>> {
         * @param def
         *            the default value when it is not present in the config file
         * 
-        * @return the associated value, or NULL if not found (not present in the
-        *         resource file)
+        * @return the associated value, or <tt>def</tt> if not found (not present
+        *         in the resource file)
         */
        public String getString(E id, String def) {
                return getString(id, def, -1);
@@ -154,22 +155,17 @@ public class Bundle<E extends Enum<E>> {
         *            the item number to get for an array of values, or -1 for
         *            non-arrays
         * 
-        * @return the associated value, or NULL if not found (not present in the
-        *         resource file)
+        * @return the associated value, <tt>def</tt> if not found (not present in
+        *         the resource file) or NULL if the item is specified (not -1) and
+        *         does not exist
         */
        public String getString(E id, String def, int item) {
                String rep = getString(id.name(), null);
                if (rep == null) {
-                       try {
-                               Meta meta = type.getDeclaredField(id.name()).getAnnotation(
-                                               Meta.class);
-                               rep = meta.def();
-                       } catch (NoSuchFieldException e) {
-                       } catch (SecurityException e) {
-                       }
+                       rep = getMetaDef(id.name());
                }
 
-               if (rep == null || rep.isEmpty()) {
+               if (rep.isEmpty()) {
                        return def;
                }
 
@@ -215,11 +211,7 @@ public class Bundle<E extends Enum<E>> {
                        setString(id.name(), value);
                } else {
                        List<String> values = getList(id);
-                       for (int i = values.size(); i < item; i++) {
-                               values.add(null);
-                       }
-                       values.set(item, value);
-                       setString(id.name(), BundleHelper.fromList(values));
+                       setString(id.name(), BundleHelper.fromList(values, value, item));
                }
        }
 
@@ -277,17 +269,15 @@ public class Bundle<E extends Enum<E>> {
         *            the id of the value to get
         * @param suffix
         *            the runtime suffix
-        * @param item
-        *            the item number to get for an array of values, or -1 for
-        *            non-arrays
         * @param def
         *            the default value when it is not present in the config file
         * @param item
         *            the item number to get for an array of values, or -1 for
         *            non-arrays
         * 
-        * @return the associated value, or NULL if not found (not present in the
-        *         resource file)
+        * @return the associated value, <tt>def</tt> if not found (not present in
+        *         the resource file), NULL if the item is specified (not -1) but
+        *         does not exist and NULL if bad key
         */
        public String getStringX(E id, String suffix, String def, int item) {
                String key = id.name()
@@ -577,6 +567,9 @@ public class Bundle<E extends Enum<E>> {
         * @param def
         *            the default value when it is not present in the config file or
         *            if it is not a char value
+        * @param item
+        *            the item number to get for an array of values, or -1 for
+        *            non-arrays
         * 
         * @return the associated value
         */
@@ -645,6 +638,9 @@ public class Bundle<E extends Enum<E>> {
         * 
         * @param id
         *            the id of the value to get
+        * @param def
+        *            the default value when it is not present in the config file or
+        *            if it is not a char value
         * 
         * @return the associated value
         */
@@ -667,6 +663,12 @@ public class Bundle<E extends Enum<E>> {
         * 
         * @param id
         *            the id of the value to get
+        * @param def
+        *            the default value when it is not present in the config file or
+        *            if it is not a char value
+        * @param item
+        *            the item number to get for an array of values, or -1 for
+        *            non-arrays
         * 
         * @return the associated value
         */
@@ -733,6 +735,9 @@ public class Bundle<E extends Enum<E>> {
         * 
         * @param id
         *            the id of the value to get
+        * @param def
+        *            the default value when it is not present in the config file or
+        *            if it is not a char value
         * 
         * @return the associated list, empty if the value is empty, NULL if it is
         *         not found or cannot be parsed as a list
@@ -754,6 +759,12 @@ public class Bundle<E extends Enum<E>> {
         * 
         * @param id
         *            the id of the value to get
+        * @param def
+        *            the default value when it is not present in the config file or
+        *            if it is not a char value
+        * @param item
+        *            the item number to get for an array of values, or -1 for
+        *            non-arrays
         * 
         * @return the associated list, empty if the value is empty, NULL if it is
         *         not found or cannot be parsed as a list
@@ -923,9 +934,35 @@ public class Bundle<E extends Enum<E>> {
                return changeMap.containsKey(key) || map.containsKey(key);
        }
 
+       /**
+        * The default {@link Meta#def()} value for the given enumeration name.
+        * 
+        * @param id
+        *            the enumeration name (the "id")
+        * 
+        * @return the def value in the {@link MetaInfo} or "" if none (never NULL)
+        */
+       protected String getMetaDef(String id) {
+               String rep = "";
+               try {
+                       Meta meta = type.getDeclaredField(id).getAnnotation(Meta.class);
+                       rep = meta.def();
+               } catch (NoSuchFieldException e) {
+               } catch (SecurityException e) {
+               }
+
+               if (rep == null) {
+                       rep = "";
+               }
+
+               return rep;
+       }
+
        /**
         * Get the value for the given key if it exists in the internal map, or
         * <tt>def</tt> if not.
+        * <p>
+        * DO NOT get the default meta value (MetaInfo.def()).
         * 
         * @param key
         *            the key to check for
@@ -1132,13 +1169,21 @@ public class Bundle<E extends Enum<E>> {
 
                boolean found = false;
                if (!resetToDefault && dir != null) {
-                       // Look into Bundles.getDirectory() for .properties files
                        try {
+                               // Look into Bundles.getDirectory() for .properties files
                                File file = getPropertyFile(dir, name.name(), locale);
                                if (file != null) {
-                                       Reader reader = new InputStreamReader(new FileInputStream(
-                                                       file), "UTF-8");
-                                       resetMap(new PropertyResourceBundle(reader));
+                                       InputStream in = new FileInputStream(file);
+                                       try {
+                                               Reader reader = new InputStreamReader(in, "UTF-8");
+                                               try {
+                                                       resetMap(new PropertyResourceBundle(reader));
+                                               } finally {
+                                                       reader.close();
+                                               }
+                                       } finally {
+                                               in.close();
+                                       }
                                        found = true;
                                }
                        } catch (IOException e) {
@@ -1167,7 +1212,7 @@ public class Bundle<E extends Enum<E>> {
        }
 
        /**
-        * Reset the backing map to the content of the given bundle, or with default
+        * Reset the backing map to the content of the given bundle, or with NULL
         * values if bundle is NULL.
         * 
         * @param bundle
@@ -1175,22 +1220,18 @@ public class Bundle<E extends Enum<E>> {
         */
        protected void resetMap(ResourceBundle bundle) {
                this.map.clear();
-               for (Field field : type.getDeclaredFields()) {
-                       try {
-                               Meta meta = field.getAnnotation(Meta.class);
-                               if (meta != null) {
-                                       E id = Enum.valueOf(type, field.getName());
-
-                                       String value;
-                                       if (bundle != null) {
-                                               value = bundle.getString(id.name());
-                                       } else {
-                                               value = meta.def();
+               if (bundle != null) {
+                       for (Field field : type.getDeclaredFields()) {
+                               try {
+                                       Meta meta = field.getAnnotation(Meta.class);
+                                       if (meta != null) {
+                                               E id = Enum.valueOf(type, field.getName());
+                                               String value = bundle.getString(id.name());
+                                               this.map.put(id.name(),
+                                                               value == null ? null : value.trim());
                                        }
-
-                                       this.map.put(id.name(), value == null ? null : value.trim());
+                               } catch (MissingResourceException e) {
                                }
-                       } catch (MissingResourceException e) {
                        }
                }
        }