package be.nikiroo.utils.resources; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; 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; /** * This class encapsulate a {@link ResourceBundle} in UTF-8. It allows to * retrieve values associated to an enumeration, and allows some additional * methods. *
* 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) {
String rep = getString(id.name(), null);
if (rep == null) {
MetaInfo
* 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
*
* @return the associated value, or NULL if not found (not present in the
* resource file)
*/
public String getStringX(E id, String suffix) {
String key = id.name()
+ (suffix == null ? "" : "_" + suffix.toUpperCase());
try {
id = Enum.valueOf(type, key);
return getString(id);
} 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) {
String key = id.name()
+ (suffix == null ? "" : "_" + suffix.toUpperCase());
try {
id = Enum.valueOf(type, key);
setString(id, value);
} 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 id
* the id of the value to get
*
* @return the associated value
*/
public Boolean getBoolean(E id) {
String str = getString(id);
return BundleHelper.parseBoolean(str);
}
/**
* 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 b = getBoolean(id);
if (b != null)
return b;
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}.
*
* 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 getInteger(E id) {
return BundleHelper.parseInteger(getString(id));
}
/**
* 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
* @param def
* the default value when it is not present in the config file or
* if it is not a int value
*
* @return the associated value
*/
public int getInteger(E id, int def) {
Integer i = getInteger(id);
if (i != null)
return i;
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}.
*
* 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));
}
/**
* 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 car = getCharacter(id);
if (car != null)
return car;
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
*
* @return the associated value
*/
public Integer getColor(E id) {
return BundleHelper.parseColor(getString(id));
}
/**
* 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) {
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
*
* @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).
*
* @param path
* the path where the .properties files are, MUST NOT be
* NULL
*
* @throws IOException
* in case of IO errors
*/
public void updateFile(String path) throws IOException {
File file = getUpdateFile(path);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file), "UTF-8"));
writeHeader(writer);
writer.write("\n");
writer.write("\n");
for (Field field : type.getDeclaredFields()) {
Meta meta = field.getAnnotation(Meta.class);
if (meta != null) {
E id = Enum.valueOf(type, field.getName());
String info = getMetaInfo(meta);
if (info != null) {
writer.write(info);
writer.write("\n");
}
writeValue(writer, id);
}
}
writer.close();
}
/**
* 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).
*
* 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