X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fresources%2FBundle.java;h=c757e2b1d705dc005b52ac425f7a9a89a14deabd;hb=d442b96e0b1453c21abf7f34bfaf73dcbed2a7ee;hp=ca61f6d8e509c2616f307f3d2076a1475575d2ad;hpb=009196a48ec0820288dac580f661c8884e1c1108;p=fanfix.git diff --git a/src/be/nikiroo/utils/resources/Bundle.java b/src/be/nikiroo/utils/resources/Bundle.java index ca61f6d..c757e2b 100644 --- a/src/be/nikiroo/utils/resources/Bundle.java +++ b/src/be/nikiroo/utils/resources/Bundle.java @@ -1,6 +1,5 @@ package be.nikiroo.utils.resources; -import java.awt.Color; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; @@ -12,60 +11,217 @@ import java.io.Reader; import java.io.Writer; import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.MissingResourceException; import java.util.PropertyResourceBundle; import java.util.ResourceBundle; +import be.nikiroo.utils.resources.Meta.Format; + /** - * This class encapsulate a {@link ResourceBundle} in UTF-8. It only allows to + * This class encapsulate a {@link ResourceBundle} in UTF-8. It allows to * retrieve values associated to an enumeration, and allows some additional * methods. - * - * @author niki + *
+ * It also sports a writable change map, and you can save back the
+ * {@link Bundle} to file with {@link Bundle#updateFile(String)}.
*
* @param
+ * 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) {
+ return getString(id, def, -1);
+ }
+
+ /**
+ * Return the value associated to the given id as a {@link String}.
+ *
+ * If no value is associated (or if it is empty!), 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
+ * @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)
+ */
+ public String getString(E id, String def, int item) {
+ String rep = getString(id.name(), null);
+ if (rep == null) {
+ rep = getMetaDef(id.name());
+ }
+
+ if (rep == null || rep.isEmpty()) {
+ return def;
+ }
+
+ if (item >= 0) {
+ List
+ * Will only accept suffixes that form an existing id.
+ *
+ * If no value is associated, take the default one if any.
*
- * @param mame
+ * @param id
* the id of the value to get
* @param suffix
* the runtime suffix
@@ -74,79 +230,224 @@ public class Bundle
+ * Will only accept suffixes that form an existing id.
+ *
+ * If no value is associated, take the default one if any.
+ *
+ * @param id
+ * the id of the value to get
+ * @param suffix
+ * the runtime suffix
+ * @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 getStringX(E id, String suffix, String def) {
+ return getStringX(id, suffix, def, -1);
+ }
+
+ /**
+ * Return the value associated to the given id as a {@link String} suffixed
+ * with the runtime value "_suffix" (that is, "_" and suffix).
+ *
+ * Will only accept suffixes that form an existing id.
+ *
+ * If no value is associated, take the default one if any.
+ *
+ * @param id
+ * the id of the value to get
+ * @param suffix
+ * the runtime suffix
+ * @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)
+ */
+ public String getStringX(E id, String suffix, String def, int item) {
String key = id.name()
+ (suffix == null ? "" : "_" + suffix.toUpperCase());
- if (containsKey(key)) {
- return getString(key).trim();
+ try {
+ id = Enum.valueOf(type, key);
+ return getString(id, def, item);
+ } catch (IllegalArgumentException e) {
}
return null;
}
+ /**
+ * Set the value associated to the given id as a {@link String} suffixed
+ * with the runtime value "_suffix" (that is, "_" and suffix).
+ *
+ * Will only accept suffixes that form an existing id.
+ *
+ * @param id
+ * the id of the value to set
+ * @param suffix
+ * the runtime suffix
+ * @param value
+ * the value
+ */
+ public void setStringX(E id, String suffix, String value) {
+ setStringX(id, suffix, value, -1);
+ }
+
+ /**
+ * Set the value associated to the given id as a {@link String} suffixed
+ * with the runtime value "_suffix" (that is, "_" and suffix).
+ *
+ * Will only accept suffixes that form an existing id.
+ *
+ * @param id
+ * the id of the value to set
+ * @param suffix
+ * the runtime suffix
+ * @param value
+ * the value
+ * @param item
+ * the item number to get for an array of values, or -1 for
+ * non-arrays
+ */
+ public void setStringX(E id, String suffix, String value, int item) {
+ String key = id.name()
+ + (suffix == null ? "" : "_" + suffix.toUpperCase());
+
+ try {
+ id = Enum.valueOf(type, key);
+ setString(id, value, item);
+ } catch (IllegalArgumentException e) {
+ }
+ }
+
/**
* Return the value associated to the given id as a {@link Boolean}.
+ *
+ * If no value is associated, take the default one if any.
*
- * @param mame
+ * @param id
* the id of the value to get
*
* @return the associated value
*/
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 BundleHelper.parseBoolean(getString(id), -1);
+ }
+ /**
+ * Return the value associated to the given id as a {@link Boolean}.
+ *
+ * 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 or
+ * if it is not a boolean value
+ *
+ * @return the associated value
+ */
+ public boolean getBoolean(E id, boolean def) {
+ Boolean value = getBoolean(id);
+ if (value != null) {
+ return value;
}
- return null;
+ return def;
}
/**
- * Return the value associated to the given id as a {@link boolean}.
+ * Return the value associated to the given id as a {@link Boolean}.
+ *
+ * If no value is associated, take the default one if any.
*
- * @param mame
+ * @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 boolean value
+ * @param item
+ * the item number to get for an array of values, or -1 for
+ * non-arrays
*
* @return the associated value
*/
- public boolean getBoolean(E id, boolean def) {
- Boolean b = getBoolean(id);
- if (b != null)
- return b;
+ public Boolean getBoolean(E id, boolean def, int item) {
+ String value = getString(id);
+ if (value != null) {
+ return BundleHelper.parseBoolean(value, item);
+ }
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) {
+ setBoolean(id, value, -1);
+ }
+
+ /**
+ * 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
+ * @param item
+ * the item number to get for an array of values, or -1 for
+ * non-arrays
+ *
+ */
+ public void setBoolean(E id, boolean value, int item) {
+ setString(id, BundleHelper.fromBoolean(value), item);
+ }
+
/**
* Return the value associated to the given id as an {@link Integer}.
+ *
+ * If no value is associated, take the default one if any.
*
- * @param mame
+ * @param id
* the id of the value to get
*
* @return the associated value
*/
public Integer getInteger(E id) {
- try {
- return Integer.parseInt(getString(id));
- } catch (Exception e) {
+ String value = getString(id);
+ if (value != null) {
+ return BundleHelper.parseInteger(value, -1);
}
return null;
}
/**
- * Return the value associated to the given id as a {@link int}.
+ * Return the value associated to the given id as an int.
+ *
+ * If no value is associated, take the default one if any.
*
- * @param mame
+ * @param id
* the id of the value to get
* @param def
* the default value when it is not present in the config file or
@@ -155,53 +456,368 @@ public class Bundle
+ * 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 or
+ * if it is not a int value
+ * @param item
+ * the item number to get for an array of values, or -1 for
+ * non-arrays
+ *
+ * @return the associated value
+ */
+ public Integer getInteger(E id, int def, int item) {
+ String value = getString(id);
+ if (value != null) {
+ return BundleHelper.parseInteger(value, item);
+ }
+
+ 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) {
+ setInteger(id, value, -1);
+ }
+
+ /**
+ * 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
+ * @param item
+ * the item number to get for an array of values, or -1 for
+ * non-arrays
+ *
+ */
+ public void setInteger(E id, int value, int item) {
+ setString(id, BundleHelper.fromInteger(value), item);
+ }
+
+ /**
+ * Return the value associated to the given id as a {@link Character}.
+ *
+ * If no value is associated, take the default one if any.
+ *
+ * @param id
+ * the id of the value to get
+ *
+ * @return the associated value
+ */
+ public Character getCharacter(E id) {
+ return BundleHelper.parseCharacter(getString(id), -1);
+ }
+
/**
* Return the value associated to the given id as a {@link Character}.
+ *
+ * If no value is associated, take the default one if any.
*
- * @param mame
+ * @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
*/
- public char getChar(E id) {
- String s = getString(id).trim();
- if (s.length() > 0) {
- return s.charAt(0);
+ public char getCharacter(E id, char def) {
+ Character value = getCharacter(id);
+ if (value != null) {
+ return value;
}
- return ' ';
+ return def;
}
/**
- * Return the value associated to the given id as a {@link Color}.
+ * Return the value associated to the given id as a {@link Character}.
+ *
+ * If no value is associated, take the default one if any.
*
- * @param the
- * id of the value to get
+ * @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
*/
- public Color getColor(E id) {
- Color color = null;
+ public Character getCharacter(E id, char def, int item) {
+ String value = getString(id);
+ if (value != null) {
+ return BundleHelper.parseCharacter(value, item);
+ }
- 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 def;
+ }
+
+ /**
+ * Set the value associated to the given id as a {@link Character}.
+ *
+ * @param id
+ * the id of the value to set
+ * @param value
+ * the value
+ *
+ */
+ public void setCharacter(E id, char value) {
+ setCharacter(id, value, -1);
+ }
+
+ /**
+ * Set the value associated to the given id as a {@link Character}.
+ *
+ * @param id
+ * the id of the value to set
+ * @param value
+ * the value
+ * @param item
+ * the item number to get for an array of values, or -1 for
+ * non-arrays
+ *
+ */
+ public void setCharacter(E id, char value, int item) {
+ setString(id, BundleHelper.fromCharacter(value), item);
+ }
+
+ /**
+ * Return the value associated to the given id as a colour if it is found
+ * and can be parsed.
+ *
+ * The returned value is an ARGB value.
+ *
+ * If no value is associated, take the default one if any.
+ *
+ * @param id
+ * the id of the value to get
+ *
+ * @return the associated value
+ */
+ public Integer getColor(E id) {
+ return BundleHelper.parseColor(getString(id), -1);
+ }
+
+ /**
+ * Return the value associated to the given id as a colour if it is found
+ * and can be parsed.
+ *
+ * The returned value is an ARGB value.
+ *
+ * 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 or
+ * if it is not a char value
+ *
+ * @return the associated value
+ */
+ public int getColor(E id, int def) {
+ Integer value = getColor(id);
+ if (value != null) {
+ return value;
+ }
+
+ return def;
+ }
+
+ /**
+ * Return the value associated to the given id as a colour if it is found
+ * and can be parsed.
+ *
+ * The returned value is an ARGB value.
+ *
+ * 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 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
+ */
+ public Integer getColor(E id, int def, int item) {
+ String value = getString(id);
+ if (value != null) {
+ return BundleHelper.parseColor(value, item);
}
- return color;
+ return def;
+ }
+
+ /**
+ * Set the value associated to the given id as a colour.
+ *
+ * The value is a BGRA value.
+ *
+ * @param id
+ * the id of the value to set
+ * @param color
+ * the new colour
+ */
+ public void setColor(E id, Integer color) {
+ setColor(id, color, -1);
+ }
+
+ /**
+ * Set the value associated to the given id as a Color.
+ *
+ * @param id
+ * the id of the value to set
+ * @param value
+ * the value
+ * @param item
+ * the item number to get for an array of values, or -1 for
+ * non-arrays
+ *
+ */
+ public void setColor(E id, int value, int item) {
+ setString(id, BundleHelper.fromColor(value), item);
+ }
+
+ /**
+ * Return the value associated to the given id as a list of values if it is
+ * found and can be parsed.
+ *
+ * If no value is associated, take the default one if any.
+ *
+ * @param id
+ * the id of the value to get
+ *
+ * @return the associated list, empty if the value is empty, NULL if it is
+ * not found or cannot be parsed as a list
+ */
+ public List
+ * 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 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
+ */
+ public List
+ * 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 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
+ */
+ public List
+ * 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).
+ *
+ * Will update the files in {@link Bundles#getDirectory()}; it MUST
+ * be set.
+ *
+ * @throws IOException
+ * in case of IO errors
+ */
+ public void updateFile() throws IOException {
+ updateFile(Bundles.getDirectory());
}
/**
@@ -212,7 +828,8 @@ public class Bundle
+ * 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).
+ *
+ * Will delete the files in {@link Bundles#getDirectory()}; it MUST
+ * be set.
+ *
+ * @return TRUE if the file was deleted
+ */
+ public boolean deleteFile() {
+ return deleteFile(Bundles.getDirectory());
+ }
+
+ /**
+ * Delete 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).
+ *
+ * @param path
+ * the path where the .properties files are, MUST NOT be
+ * NULL
+ *
+ * @return TRUE if the file was deleted
+ */
+ public boolean deleteFile(String path) {
+ File file = getUpdateFile(path);
+ return file.delete();
+ }
+
+ /**
+ * The description {@link TransBundle}, that is, a {@link TransBundle}
+ * dedicated to the description of the values of the given {@link Bundle}
+ * (can be NULL).
+ *
+ * @return the description {@link TransBundle}
+ */
+ public TransBundle
+ * DO NOT get the default meta value (MetaInfo.def()).
*
* @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 def if not found
*/
- protected String getString(String key) {
- if (containsKey(key)) {
- return map.getString(key);
+ protected String getString(String key, String def) {
+ if (changeMap.containsKey(key)) {
+ return changeMap.get(key);
}
- return null;
+ if (map.containsKey(key)) {
+ return map.get(key);
+ }
+
+ return def;
+ }
+
+ /**
+ * Set the value for this key, in the change map (it is kept in memory, not
+ * yet on disk).
+ *
+ * @param key
+ * the key
+ * @param value
+ * the associated value
+ */
+ protected void setString(String key, String value) {
+ changeMap.put(key, value == null ? null : value.trim());
}
/**
@@ -296,44 +1003,49 @@ public class Bundle
+ * Will prepend a # sign if the is is not set (see
+ * {@link Bundle#isSet(Enum, boolean)}).
*
* @param writer
* the {@link Writer} to write into
@@ -374,12 +1089,15 @@ public class Bundle
+ * Will prepend a # sign if the is is not set.
*
* @param writer
* the {@link Writer} to write into
@@ -387,12 +1105,19 @@ public class Bundle