X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fresources%2FBundle.java;h=3506fdde070d82f0e24d803412a68e17523e0333;hb=2e465ed3dc72efcdf96e419b39d6119c0cc41c6c;hp=1c63d69f04f84276eeaf19ee3a29e028f52fae54;hpb=ec1f3444e9f238ce1559d5fff32eb5a7ab8aba53;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/resources/Bundle.java b/src/be/nikiroo/utils/resources/Bundle.java index 1c63d69..fe3ac1a 100644 --- a/src/be/nikiroo/utils/resources/Bundle.java +++ b/src/be/nikiroo/utils/resources/Bundle.java @@ -5,70 +5,225 @@ 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; -import java.io.UnsupportedEncodingException; 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.Bundles; -import be.nikiroo.utils.resources.Meta; +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 def 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, def 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) {
+ rep = getMetaDef(id.name());
+ }
+
+ if (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
@@ -77,80 +232,225 @@ 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, def 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()
- + ((suffix == null || suffix.isEmpty()) ? "" : "_"
- + suffix.toUpperCase());
+ + (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
@@ -159,38 +459,380 @@ 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 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 getCharacter(E id, char def) {
+ Character value = getCharacter(id);
+ if (value != null) {
+ return value;
+ }
return def;
}
/**
* 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
+ * @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 Character getCharacter(E id, char def, int item) {
+ String value = getString(id);
+ if (value != null) {
+ return BundleHelper.parseCharacter(value, item);
+ }
+
+ 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 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 int getColor(E id, int def) {
+ Integer value = getColor(id);
+ if (value != null) {
+ return value;
}
- return ' ';
+ 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 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());
}
/**
- * 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).
+ * 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).
*
* @param path
- * the path where the .properties files are
+ * the path where the .properties files are, MUST NOT be
+ * NULL
*
* @throws IOException
* in case of IO errors
@@ -208,7 +850,7 @@ 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 true if it does
+ * @return the value, or def if not found
*/
- protected String getString(String key) {
- if (containsKey(key)) {
- try {
- // Note: it is also possible to fix the borked charset issue
- // with a custom ResourceBundle#Control class, but this one,
- // while a workaround, depend less upon the JRE classes, which
- // may change
- return new String(map.getString(key).getBytes("ISO-8859-1"),
- "UTF-8");
- } catch (UnsupportedEncodingException e) {
- // Those 2 encodings are always supported
- e.printStackTrace();
- }
+ 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());
}
/**
@@ -276,44 +1006,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
@@ -354,12 +1092,15 @@ public class Bundle
+ * Will prepend a # sign if the is is not set.
*
* @param writer
* the {@link Writer} to write into
@@ -367,12 +1108,19 @@ public class Bundle