Fix \t handling bug in Bundle
[nikiroo-utils.git] / src / be / nikiroo / utils / resources / Bundle.java
index 78d73a8148088619180caa59aa50fffcc2d50d40..ca61f6d8e509c2616f307f3d2076a1475575d2ad 100644 (file)
@@ -1,5 +1,6 @@
 package be.nikiroo.utils.resources;
 
+import java.awt.Color;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileInputStream;
@@ -57,7 +58,7 @@ public class Bundle<E extends Enum<E>> {
         *         resource file)
         */
        public String getString(E id) {
-               return getStringX(id, "");
+               return getStringX(id, null);
        }
 
        /**
@@ -74,8 +75,7 @@ public class Bundle<E extends Enum<E>> {
         */
        public String getStringX(E id, String suffix) {
                String key = id.name()
-                               + ((suffix == null || suffix.isEmpty()) ? "" : "_"
-                                               + suffix.toUpperCase());
+                               + (suffix == null ? "" : "_" + suffix.toUpperCase());
 
                if (containsKey(key)) {
                        return getString(key).trim();
@@ -180,10 +180,36 @@ public class Bundle<E extends Enum<E>> {
        }
 
        /**
-        * Create/update the .properties file. Will use the most likely candidate as
-        * base if the file does not already exists and this resource is
-        * translatable (for instance, "en_US" will use "en" as a base if the
-        * resource is a translation file).
+        * Return the value associated to the given id as a {@link Color}.
+        * 
+        * @param the
+        *            id of the value to get
+        * 
+        * @return the associated value
+        */
+       public Color getColor(E id) {
+               Color color = null;
+
+               String bg = getString(id).trim();
+               if (bg.startsWith("#") && bg.length() == 7) {
+                       try {
+                               color = new Color(Integer.parseInt(bg.substring(1, 3), 16),
+                                               Integer.parseInt(bg.substring(3, 5), 16),
+                                               Integer.parseInt(bg.substring(5, 7), 16));
+                       } catch (NumberFormatException e) {
+                               color = null; // no changes
+                       }
+               }
+
+               return color;
+       }
+
+       /**
+        * Create/update the .properties file.
+        * <p>
+        * Will use the most likely candidate as base if the file does not already
+        * exists and this resource is translatable (for instance, "en_US" will use
+        * "en" as a base if the resource is a translation file).
         * 
         * @param path
         *            the path where the .properties files are
@@ -219,6 +245,13 @@ public class Bundle<E extends Enum<E>> {
                writer.close();
        }
 
+       /**
+        * Reload the {@link Bundle} data files.
+        */
+       public void reload() {
+               setBundle(name, null);
+       }
+
        /**
         * Check if the internal map contains the given key.
         * 
@@ -367,7 +400,7 @@ public class Bundle<E extends Enum<E>> {
                        value = "";
                }
 
-               String[] lines = value.replaceAll("\\\t", "\\\\\\t").split("\n");
+               String[] lines = value.replaceAll("\t", "\\\\\\t").split("\n");
                for (int i = 0; i < lines.length; i++) {
                        writer.write(lines[i]);
                        if (i < lines.length - 1) {