X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fresources%2FBundle.java;h=3506fdde070d82f0e24d803412a68e17523e0333;hb=b15a49851835d45d35ba9fdd28521df34021828f;hp=424f12096dbfaa4378eda21208118be59e0ae06a;hpb=3bfc1d20054f0a97f268f1fffea29df10ae14b42;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/resources/Bundle.java b/src/be/nikiroo/utils/resources/Bundle.java index 424f120..3506fdd 100644 --- a/src/be/nikiroo/utils/resources/Bundle.java +++ b/src/be/nikiroo/utils/resources/Bundle.java @@ -19,6 +19,8 @@ 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 allows to * retrieve values associated to an enumeration, and allows some additional @@ -72,6 +74,42 @@ public class Bundle> { 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 isSet(E id, boolean includeDefaultValue) { + return isSet(id.name(), includeDefaultValue); + } + + /** + * 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 + */ + protected boolean isSet(String name, boolean includeDefaultValue) { + if (getString(name, null) == null) { + if (!includeDefaultValue || getString(name, "") == null) { + return false; + } + } + + return true; + } + /** * Return the value associated to the given id as a {@link String}. * @@ -82,7 +120,39 @@ public class Bundle> { * resource file) */ public String getString(E id) { - return getString(id.name()); + return getString(id, null); + } + + /** + * Return the value associated to the given id as a {@link String}. + *

+ * 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) { + try { + Meta meta = type.getDeclaredField(id.name()).getAnnotation( + Meta.class); + rep = meta.def(); + } catch (NoSuchFieldException e) { + } catch (SecurityException e) { + } + } + + if (rep == null) { + rep = def; + } + + return rep; } /** @@ -103,6 +173,8 @@ public class Bundle> { * 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 @@ -153,6 +225,8 @@ public class Bundle> { /** * 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 @@ -166,6 +240,8 @@ public class Bundle> { /** * 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 @@ -182,7 +258,7 @@ public class Bundle> { return def; } - + /** * Set the value associated to the given id as a {@link Boolean}. * @@ -196,9 +272,10 @@ public class Bundle> { setString(id.name(), BundleHelper.fromBoolean(value)); } - /** * Return the value associated to the given id as an {@link Integer}. + *

+ * If no value is associated, take the default one if any. * * @param id * the id of the value to get @@ -211,6 +288,8 @@ public class Bundle> { /** * Return the value associated to the given id as an int. + *

+ * If no value is associated, take the default one if any. * * @param id * the id of the value to get @@ -240,9 +319,11 @@ public class Bundle> { 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}. + *

+ * If no value is associated, take the default one if any. * * @param id * the id of the value to get @@ -255,6 +336,8 @@ public class Bundle> { /** * 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 @@ -277,6 +360,8 @@ public class Bundle> { * 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 @@ -298,12 +383,14 @@ public class Bundle> { * the new colour */ public void setColor(E id, Integer color) { - setString(id, BundleHelper.fromColour(color)); + 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. + *

+ * If no value is associated, take the default one if any. * * @param id * the id of the value to get @@ -456,15 +543,17 @@ public class Bundle> { } /** - * 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 + * def 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 def if not found */ - protected String getString(String key) { + protected String getString(String key, String def) { if (changeMap.containsKey(key)) { return changeMap.get(key); } @@ -473,7 +562,7 @@ public class Bundle> { return map.get(key); } - return null; + return def; } /** @@ -505,37 +594,40 @@ public class Bundle> { 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 == Format.STRING) { return null; } StringBuilder builder = new StringBuilder(); - builder.append("# ").append(desc); - if (desc.length() > 20) { - builder.append("\n#"); + for (String line : desc.split("\n")) { + builder.append("# ").append(line).append("\n"); } if (group) { - builder.append("This item is used as a group, its content is not expected to be used."); + builder.append("# This item is used as a group, its content is not expected to be used."); } else { - builder.append(" (FORMAT: ").append(format) - .append(nullable ? "" : " (required)"); - builder.append(") ").append(info); + builder.append("# (FORMAT: ").append(format) + .append(nullable ? "" : ", required"); + builder.append(") "); if (list.length > 0) { - builder.append("\n# ALLOWED VALUES:"); + builder.append("\n# ALLOWED VALUES: "); + boolean first = true; for (String value : list) { - builder.append(" \"").append(value).append("\""); + if (!first) { + builder.append(", "); + } + builder.append(BundleHelper.escape(value)); + first = false; } } if (array) { - builder.append("\n# (This item accept a list of comma-separated values)"); + builder.append("\n# (This item accepts a list of escaped comma-separated values)"); } } @@ -567,8 +659,11 @@ public class Bundle> { } /** - * Write the given id to the config file, i.e., "MY_ID = my_curent_value" - * followed by a new line + * Write the given data to the config file, i.e., "MY_ID = my_curent_value" + * followed by a new line. + *

+ * Will prepend a # sign if the is is not set (see + * {@link Bundle#isSet(Enum, boolean)}). * * @param writer * the {@link Writer} to write into @@ -579,12 +674,15 @@ public class Bundle> { * in case of IO error */ protected void writeValue(Writer writer, E id) throws IOException { - writeValue(writer, id.name(), getString(id)); + boolean set = isSet(id, false); + writeValue(writer, id.name(), getString(id), set); } /** * Write the given data to the config file, i.e., "MY_ID = my_curent_value" - * followed by a new line + * followed by a new line. + *

+ * Will prepend a # sign if the is is not set. * * @param writer * the {@link Writer} to write into @@ -592,12 +690,19 @@ public class Bundle> { * the id to write * @param value * the id's value + * @param set + * the value is set in this {@link Bundle} * * @throws IOException * in case of IO error */ - protected void writeValue(Writer writer, String id, String value) - throws IOException { + protected void writeValue(Writer writer, String id, String value, + boolean set) throws IOException { + + if (!set) { + writer.write('#'); + } + writer.write(id); writer.write(" = "); @@ -651,7 +756,7 @@ public class Bundle> { File file = getPropertyFile(dir, name.name(), locale); if (file != null) { Reader reader = new InputStreamReader(new FileInputStream( - file), "UTF8"); + file), "UTF-8"); resetMap(new PropertyResourceBundle(reader)); found = true; } @@ -682,7 +787,7 @@ public class Bundle> { /** * Reset the backing map to the content of the given bundle, or with default - * valiues if bundle is NULL. + * values if bundle is NULL. * * @param bundle * the bundle to copy