package be.nikiroo.utils.resources; import java.awt.Color; 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
* Will only accept suffixes that form an existing id.
*
* @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 get
* @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}.
*
* @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 null;
}
/**
* Return the value associated to the given id as a {@link Boolean}.
*
* @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;
}
/**
* Return the value associated to the given id as an {@link Integer}.
*
* @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) {
}
return null;
}
/**
* Return the value associated to the given id as an int.
*
* @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;
}
/**
* Return the value associated to the given id as a {@link Character}.
*
* @param id
* the id of the value to get
*
* @return the associated value
*/
public Character getCharacter(E id) {
String s = getString(id).trim();
if (s.length() > 0) {
return s.charAt(0);
}
return null;
}
/**
* Return the value associated to the given id as a {@link Character}.
*
* @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) {
String s = getString(id).trim();
if (s.length() > 0) {
return s.charAt(0);
}
return def;
}
/**
* Return the value associated to the given id as a {@link Color}.
*
* @param id
* 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 || bg.length() == 9)) {
try {
int r = Integer.parseInt(bg.substring(1, 3), 16);
int g = Integer.parseInt(bg.substring(3, 5), 16);
int b = Integer.parseInt(bg.substring(5, 7), 16);
int a = 255;
if (bg.length() == 9) {
a = Integer.parseInt(bg.substring(7, 9), 16);
}
color = new Color(r, g, b, a);
} catch (NumberFormatException e) {
color = null; // no changes
}
}
// Try by name if still not found
if (color == null) {
try {
Field field = Color.class.getField(bg);
color = (Color) field.get(null);
} catch (Exception e) {
}
}
//
return color;
}
/**
* Set the value associated to the given id as a {@link Color}.
*
* @param id
* the id of the value to set
* @param color
* the new color
*/
public void setColor(E id, Color color) {
// Check for named colours first
try {
Field[] fields = Color.class.getFields();
for (Field field : fields) {
if (field.equals(color)) {
setString(id, field.getName());
return;
}
}
} catch (Exception e) {
}
//
String r = Integer.toString(color.getRed(), 16);
String g = Integer.toString(color.getGreen(), 16);
String b = Integer.toString(color.getBlue(), 16);
String a = "";
if (color.getAlpha() < 255) {
a = Integer.toString(color.getAlpha(), 16);
}
setString(id, "#" + r + g + b + a);
}
/**
* 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).
*
* 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();
}
/**
* 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