Bundle: use default Meta value if set
[nikiroo-utils.git] / src / be / nikiroo / utils / resources / Bundle.java
index f01aa445be5b36f8640f9b313a772301e6629581..09481c4acf56af0611da019f3e3f63cd0543b988 100644 (file)
@@ -72,6 +72,27 @@ public class Bundle<E extends Enum<E>> {
                setBundle(name, Locale.getDefault(), false);
        }
 
+       /**
+        * Check if the setting is set into this {@link Bundle}.
+        * 
+        * @param id
+        *            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
+        * 
+        * @return TRUE if the setting is set
+        */
+       public boolean iSet(E id, boolean includeDefaultValue) {
+               if (getString(id.toString(), null) == null) {
+                       if (!includeDefaultValue || getString(id) == null) {
+                               return false;
+                       }
+               }
+
+               return true;
+       }
+
        /**
         * Return the value associated to the given id as a {@link String}.
         * 
@@ -82,14 +103,41 @@ public class Bundle<E extends Enum<E>> {
         *         resource file)
         */
        public String getString(E id) {
-               return getString(id.name());
+               return getString(id, null);
        }
 
        /**
-        * Set the value associated to the given id as a {@link String}.
+        * Return the value associated to the given id as a {@link String}.
+        * <p>
+        * If no value is associated, take the default one if any.
         * 
         * @param id
         *            the id of the value to get
+        * @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)
+        */
+       public String getString(E id, String def) {
+               String rep = getString(id.name(), null);
+               if (rep == null) {
+                       MetaInfo<E> info = new MetaInfo<E>(type, this, id);
+                       rep = info.getDefaultString();
+               }
+
+               if (rep == null) {
+                       rep = def;
+               }
+
+               return rep;
+       }
+
+       /**
+        * Set the value associated to the given id as a {@link String}.
+        * 
+        * @param id
+        *            the id of the value to set
         * @param value
         *            the value
         * 
@@ -103,6 +151,8 @@ public class Bundle<E extends Enum<E>> {
         * with the runtime value "_suffix" (that is, "_" and suffix).
         * <p>
         * Will only accept suffixes that form an existing id.
+        * <p>
+        * If no value is associated, take the default one if any.
         * 
         * @param id
         *            the id of the value to get
@@ -133,7 +183,7 @@ public class Bundle<E extends Enum<E>> {
         * Will only accept suffixes that form an existing id.
         * 
         * @param id
-        *            the id of the value to get
+        *            the id of the value to set
         * @param suffix
         *            the runtime suffix
         * @param value
@@ -153,6 +203,8 @@ public class Bundle<E extends Enum<E>> {
 
        /**
         * Return the value associated to the given id as a {@link Boolean}.
+        * <p>
+        * If no value is associated, take the default one if any.
         * 
         * @param id
         *            the id of the value to get
@@ -161,21 +213,13 @@ public class Bundle<E extends Enum<E>> {
         */
        public Boolean getBoolean(E id) {
                String str = getString(id);
-               if (str != null && str.length() > 0) {
-                       if (str.equalsIgnoreCase("true") || str.equalsIgnoreCase("on")
-                                       || str.equalsIgnoreCase("yes"))
-                               return true;
-                       if (str.equalsIgnoreCase("false") || str.equalsIgnoreCase("off")
-                                       || str.equalsIgnoreCase("no"))
-                               return false;
-
-               }
-
-               return null;
+               return BundleHelper.parseBoolean(str);
        }
 
        /**
         * Return the value associated to the given id as a {@link Boolean}.
+        * <p>
+        * If no value is associated, take the default one if any.
         * 
         * @param id
         *            the id of the value to get
@@ -193,8 +237,23 @@ public class Bundle<E extends Enum<E>> {
                return def;
        }
 
+       /**
+        * Set the value associated to the given id as a {@link Boolean}.
+        * 
+        * @param id
+        *            the id of the value to set
+        * @param value
+        *            the value
+        * 
+        */
+       public void setBoolean(E id, boolean value) {
+               setString(id.name(), BundleHelper.fromBoolean(value));
+       }
+
        /**
         * Return the value associated to the given id as an {@link Integer}.
+        * <p>
+        * If no value is associated, take the default one if any.
         * 
         * @param id
         *            the id of the value to get
@@ -202,16 +261,13 @@ public class Bundle<E extends Enum<E>> {
         * @return the associated value
         */
        public Integer getInteger(E id) {
-               try {
-                       return Integer.parseInt(getString(id));
-               } catch (Exception e) {
-               }
-
-               return null;
+               return BundleHelper.parseInteger(getString(id));
        }
 
        /**
         * Return the value associated to the given id as an int.
+        * <p>
+        * If no value is associated, take the default one if any.
         * 
         * @param id
         *            the id of the value to get
@@ -229,8 +285,23 @@ public class Bundle<E extends Enum<E>> {
                return def;
        }
 
+       /**
+        * Set the value associated to the given id as a {@link Integer}.
+        * 
+        * @param id
+        *            the id of the value to set
+        * @param value
+        *            the value
+        * 
+        */
+       public void setInteger(E id, int value) {
+               setString(id.name(), BundleHelper.fromInteger(value));
+       }
+
        /**
         * Return the value associated to the given id as a {@link Character}.
+        * <p>
+        * If no value is associated, take the default one if any.
         * 
         * @param id
         *            the id of the value to get
@@ -238,16 +309,13 @@ public class Bundle<E extends Enum<E>> {
         * @return the associated value
         */
        public Character getCharacter(E id) {
-               String s = getString(id).trim();
-               if (s.length() > 0) {
-                       return s.charAt(0);
-               }
-
-               return null;
+               return BundleHelper.parseCharacter(getString(id));
        }
 
        /**
         * Return the value associated to the given id as a {@link Character}.
+        * <p>
+        * If no value is associated, take the default one if any.
         * 
         * @param id
         *            the id of the value to get
@@ -258,10 +326,9 @@ public class Bundle<E extends Enum<E>> {
         * @return the associated value
         */
        public char getCharacter(E id, char def) {
-               String s = getString(id);
-               if (s != null && s.length() > 0) {
-                       return s.trim().charAt(0);
-               }
+               Character car = getCharacter(id);
+               if (car != null)
+                       return car;
 
                return def;
        }
@@ -271,6 +338,8 @@ public class Bundle<E extends Enum<E>> {
         * and can be parsed.
         * <p>
         * The returned value is an ARGB value.
+        * <p>
+        * If no value is associated, take the default one if any.
         * 
         * @param id
         *            the id of the value to get
@@ -278,92 +347,13 @@ public class Bundle<E extends Enum<E>> {
         * @return the associated value
         */
        public Integer getColor(E id) {
-               Integer rep = null;
-
-               String bg = getString(id).trim();
-
-               int r = 0, g = 0, b = 0, a = -1;
-               if (bg.startsWith("#") && (bg.length() == 7 || bg.length() == 9)) {
-                       try {
-                               r = Integer.parseInt(bg.substring(1, 3), 16);
-                               g = Integer.parseInt(bg.substring(3, 5), 16);
-                               b = Integer.parseInt(bg.substring(5, 7), 16);
-                               if (bg.length() == 9) {
-                                       a = Integer.parseInt(bg.substring(7, 9), 16);
-                               } else {
-                                       a = 255;
-                               }
-
-                       } catch (NumberFormatException e) {
-                               // no changes
-                       }
-               }
-
-               // Try by name if still not found
-               if (a == -1) {
-                       if ("black".equalsIgnoreCase(bg)) {
-                               a = 255;
-                               r = 0;
-                               g = 0;
-                               b = 0;
-                       } else if ("white".equalsIgnoreCase(bg)) {
-                               a = 255;
-                               r = 255;
-                               g = 255;
-                               b = 255;
-                       } else if ("red".equalsIgnoreCase(bg)) {
-                               a = 255;
-                               r = 255;
-                               g = 0;
-                               b = 0;
-                       } else if ("green".equalsIgnoreCase(bg)) {
-                               a = 255;
-                               r = 0;
-                               g = 255;
-                               b = 0;
-                       } else if ("blue".equalsIgnoreCase(bg)) {
-                               a = 255;
-                               r = 0;
-                               g = 0;
-                               b = 255;
-                       } else if ("grey".equalsIgnoreCase(bg)
-                                       || "gray".equalsIgnoreCase(bg)) {
-                               a = 255;
-                               r = 128;
-                               g = 128;
-                               b = 128;
-                       } else if ("cyan".equalsIgnoreCase(bg)) {
-                               a = 255;
-                               r = 0;
-                               g = 255;
-                               b = 255;
-                       } else if ("magenta".equalsIgnoreCase(bg)) {
-                               a = 255;
-                               r = 255;
-                               g = 0;
-                               b = 255;
-                       } else if ("yellow".equalsIgnoreCase(bg)) {
-                               a = 255;
-                               r = 255;
-                               g = 255;
-                               b = 0;
-                       }
-               }
-
-               if (a != -1) {
-                       rep = ((a & 0xFF) << 24) //
-                                       | ((r & 0xFF) << 16) //
-                                       | ((g & 0xFF) << 8) //
-                                       | ((b & 0xFF) << 0);
-               }
-
-               return rep;
+               return BundleHelper.parseColor(getString(id));
        }
 
        /**
         * Set the value associated to the given id as a colour.
         * <p>
-        * The value is an BGRA value.
+        * The value is a BGRA value.
         * 
         * @param id
         *            the id of the value to set
@@ -371,25 +361,14 @@ public class Bundle<E extends Enum<E>> {
         *            the new colour
         */
        public void setColor(E id, Integer color) {
-               int a = (color >> 24) & 0xFF;
-               int r = (color >> 16) & 0xFF;
-               int g = (color >> 8) & 0xFF;
-               int b = (color >> 0) & 0xFF;
-
-               String rs = Integer.toString(r, 16);
-               String gs = Integer.toString(g, 16);
-               String bs = Integer.toString(b, 16);
-               String as = "";
-               if (a < 255) {
-                       as = Integer.toString(a, 16);
-               }
-
-               setString(id, "#" + rs + gs + bs + as);
+               setString(id, BundleHelper.fromColor(color));
        }
 
        /**
         * Return the value associated to the given id as a list of values if it is
         * found and can be parsed.
+        * <p>
+        * If no value is associated, take the default one if any.
         * 
         * @param id
         *            the id of the value to get
@@ -398,66 +377,7 @@ public class Bundle<E extends Enum<E>> {
         *         not found or cannot be parsed as a list
         */
        public List<String> getList(E id) {
-               List<String> list = new ArrayList<String>();
-               String raw = getString(id);
-               try {
-                       boolean inQuote = false;
-                       boolean prevIsBackSlash = false;
-                       StringBuilder builder = new StringBuilder();
-                       for (int i = 0; i < raw.length(); i++) {
-                               char car = raw.charAt(i);
-
-                               if (prevIsBackSlash) {
-                                       builder.append(car);
-                                       prevIsBackSlash = false;
-                               } else {
-                                       switch (car) {
-                                       case '"':
-                                               if (inQuote) {
-                                                       list.add(builder.toString());
-                                                       builder.setLength(0);
-                                               }
-
-                                               inQuote = !inQuote;
-                                               break;
-                                       case '\\':
-                                               prevIsBackSlash = true;
-                                               break;
-                                       case ' ':
-                                       case '\n':
-                                       case '\r':
-                                               if (inQuote) {
-                                                       builder.append(car);
-                                               }
-                                               break;
-
-                                       case ',':
-                                               if (!inQuote) {
-                                                       break;
-                                               }
-                                               // continue to default
-                                       default:
-                                               if (!inQuote) {
-                                                       // Bad format!
-                                                       return null;
-                                               }
-
-                                               builder.append(car);
-                                               break;
-                                       }
-                               }
-                       }
-
-                       if (inQuote || prevIsBackSlash) {
-                               // Bad format!
-                               return null;
-                       }
-
-               } catch (Exception e) {
-                       return null;
-               }
-
-               return list;
+               return BundleHelper.parseList(getString(id));
        }
 
        /**
@@ -469,17 +389,7 @@ public class Bundle<E extends Enum<E>> {
         *            the new list of values
         */
        public void setList(E id, List<String> list) {
-               StringBuilder builder = new StringBuilder();
-               for (String item : list) {
-                       if (builder.length() > 0) {
-                               builder.append(", ");
-                       }
-                       builder.append('"')//
-                                       .append(item.replace("\\", "\\\\").replace("\"", "\\\""))//
-                                       .append('"');
-               }
-
-               setString(id, builder.toString());
+               setString(id, BundleHelper.fromList(list));
        }
 
        /**
@@ -611,15 +521,17 @@ public class Bundle<E extends Enum<E>> {
        }
 
        /**
-        * Get the value for the given key if it exists in the internal map, or NULL
-        * if not.
+        * Get the value for the given key if it exists in the internal map, or
+        * <tt>def</tt> if not.
         * 
         * @param key
         *            the key to check for
+        * @param def
+        *            the default value when it is not present in the internal map
         * 
-        * @return the value, or NULL
+        * @return the value, or <tt>def</tt> if not found
         */
-       protected String getString(String key) {
+       protected String getString(String key, String def) {
                if (changeMap.containsKey(key)) {
                        return changeMap.get(key);
                }
@@ -628,7 +540,7 @@ public class Bundle<E extends Enum<E>> {
                        return map.get(key);
                }
 
-               return null;
+               return def;
        }
 
        /**
@@ -660,12 +572,11 @@ public class Bundle<E extends Enum<E>> {
                String[] list = meta.list();
                boolean nullable = meta.nullable();
                String def = meta.def();
-               String info = meta.info();
                boolean array = meta.array();
 
                // Default, empty values -> NULL
-               if (desc.length() + list.length + info.length() + def.length() == 0
-                               && !group && nullable && format == Meta.Format.STRING) {
+               if (desc.length() + list.length + def.length() == 0 && !group
+                               && nullable && format == Meta.Format.STRING) {
                        return null;
                }
 
@@ -680,7 +591,7 @@ public class Bundle<E extends Enum<E>> {
                } else {
                        builder.append(" (FORMAT: ").append(format)
                                        .append(nullable ? "" : " (required)");
-                       builder.append(") ").append(info);
+                       builder.append(") ");
 
                        if (list.length > 0) {
                                builder.append("\n# ALLOWED VALUES:");