* Set the value associated to the given id as a {@link String}.
*
* @param id
- * the id of the value to get
+ * the id of the value to set
* @param value
* the value
*
* Will only accept suffixes that form an existing id.
*
* @param id
- * the id of the value to get
+ * the id of the value to set
* @param suffix
* the runtime suffix
* @param 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 BundleHelper.parseBoolean(str);
}
/**
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}.
* @return the associated value
*/
public Integer getInteger(E id) {
- try {
- return Integer.parseInt(getString(id));
- } catch (Exception e) {
- }
-
- return null;
+ return BundleHelper.parseInteger(getString(id));
}
/**
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}.
*
* @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 BundleHelper.parseCharacter(getString(id));
}
/**
* @return the associated value
*/
public char getCharacter(E id, char def) {
- String s = getString(id);
- if (s != null && s.length() > 0) {
- return s.trim().charAt(0);
- }
+ Character car = getCharacter(id);
+ if (car != null)
+ return car;
return def;
}
* @return the associated value
*/
public Integer getColor(E id) {
- Integer rep = null;
-
- String bg = getString(id).trim();
-
- int r = 0, g = 0, b = 0, a = -1;
- if (bg.startsWith("#") && (bg.length() == 7 || bg.length() == 9)) {
- try {
- r = Integer.parseInt(bg.substring(1, 3), 16);
- g = Integer.parseInt(bg.substring(3, 5), 16);
- b = Integer.parseInt(bg.substring(5, 7), 16);
- if (bg.length() == 9) {
- a = Integer.parseInt(bg.substring(7, 9), 16);
- } else {
- a = 255;
- }
-
- } catch (NumberFormatException e) {
- // no changes
- }
- }
-
- // Try by name if still not found
- if (a == -1) {
- if ("black".equalsIgnoreCase(bg)) {
- a = 255;
- r = 0;
- g = 0;
- b = 0;
- } else if ("white".equalsIgnoreCase(bg)) {
- a = 255;
- r = 255;
- g = 255;
- b = 255;
- } else if ("red".equalsIgnoreCase(bg)) {
- a = 255;
- r = 255;
- g = 0;
- b = 0;
- } else if ("green".equalsIgnoreCase(bg)) {
- a = 255;
- r = 0;
- g = 255;
- b = 0;
- } else if ("blue".equalsIgnoreCase(bg)) {
- a = 255;
- r = 0;
- g = 0;
- b = 255;
- } else if ("grey".equalsIgnoreCase(bg)
- || "gray".equalsIgnoreCase(bg)) {
- a = 255;
- r = 128;
- g = 128;
- b = 128;
- } else if ("cyan".equalsIgnoreCase(bg)) {
- a = 255;
- r = 0;
- g = 255;
- b = 255;
- } else if ("magenta".equalsIgnoreCase(bg)) {
- a = 255;
- r = 255;
- g = 0;
- b = 255;
- } else if ("yellow".equalsIgnoreCase(bg)) {
- a = 255;
- r = 255;
- g = 255;
- b = 0;
- }
- }
-
- if (a != -1) {
- rep = ((a & 0xFF) << 24) //
- | ((r & 0xFF) << 16) //
- | ((g & 0xFF) << 8) //
- | ((b & 0xFF) << 0);
- }
-
- return rep;
+ return BundleHelper.parseColor(getString(id));
}
/**
* Set the value associated to the given id as a colour.
* <p>
- * The value is an BGRA value.
+ * The value is a BGRA value.
*
* @param id
* the id of the value to set
* the new colour
*/
public void setColor(E id, Integer color) {
- int a = (color >> 24) & 0xFF;
- int r = (color >> 16) & 0xFF;
- int g = (color >> 8) & 0xFF;
- int b = (color >> 0) & 0xFF;
-
- String rs = Integer.toString(r, 16);
- String gs = Integer.toString(g, 16);
- String bs = Integer.toString(b, 16);
- String as = "";
- if (a < 255) {
- as = Integer.toString(a, 16);
- }
-
- setString(id, "#" + rs + gs + bs + as);
+ setString(id, BundleHelper.fromColour(color));
}
/**
* not found or cannot be parsed as a list
*/
public List<String> getList(E id) {
- List<String> list = new ArrayList<String>();
- String raw = getString(id);
- try {
- boolean inQuote = false;
- boolean prevIsBackSlash = false;
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < raw.length(); i++) {
- char car = raw.charAt(i);
-
- if (prevIsBackSlash) {
- builder.append(car);
- prevIsBackSlash = false;
- } else {
- switch (car) {
- case '"':
- if (inQuote) {
- list.add(builder.toString());
- builder.setLength(0);
- }
-
- inQuote = !inQuote;
- break;
- case '\\':
- prevIsBackSlash = true;
- break;
- case ' ':
- case '\n':
- case '\r':
- if (inQuote) {
- builder.append(car);
- }
- break;
-
- case ',':
- if (!inQuote) {
- break;
- }
- // continue to default
- default:
- if (!inQuote) {
- // Bad format!
- return null;
- }
-
- builder.append(car);
- break;
- }
- }
- }
-
- if (inQuote || prevIsBackSlash) {
- // Bad format!
- return null;
- }
-
- } catch (Exception e) {
- return null;
- }
-
- return list;
+ return BundleHelper.parseList(getString(id));
}
/**
* the new list of values
*/
public void setList(E id, List<String> list) {
- StringBuilder builder = new StringBuilder();
- for (String item : list) {
- if (builder.length() > 0) {
- builder.append(", ");
- }
- builder.append('"')//
- .append(item.replace("\\", "\\\\").replace("\"", "\\\""))//
- .append('"');
- }
-
- setString(id, builder.toString());
+ setString(id, BundleHelper.fromList(list));
}
/**
--- /dev/null
+package be.nikiroo.utils.resources;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Internal class used to convert data to/from {@link String}s in the context of
+ * {@link Bundle}s.
+ *
+ * @author niki
+ */
+class BundleHelper {
+ /**
+ * Convert the given {@link String} into a {@link Boolean} if it represents
+ * a {@link Boolean}, or NULL if it doesn't.
+ * <p>
+ * Note: null, "strange text", ""... will all be converted to NULL.
+ *
+ * @param str
+ * the input {@link String}
+ *
+ * @return the converted {@link Boolean} or NULL
+ */
+ static public Boolean parseBoolean(String str) {
+ 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 a {@link String} representation of the given {@link Boolean}.
+ *
+ * @param value
+ * the input value
+ *
+ * @return the raw {@link String} value that correspond to it
+ */
+ static public String fromBoolean(boolean value) {
+ return Boolean.toString(value);
+ }
+
+ /**
+ * Convert the given {@link String} into a {@link Integer} if it represents
+ * a {@link Integer}, or NULL if it doesn't.
+ * <p>
+ * Note: null, "strange text", ""... will all be converted to NULL.
+ *
+ * @param str
+ * the input {@link String}
+ *
+ * @return the converted {@link Integer} or NULL
+ */
+ static public Integer parseInteger(String str) {
+ try {
+ return Integer.parseInt(str);
+ } catch (Exception e) {
+ }
+
+ return null;
+ }
+
+ /**
+ * Return a {@link String} representation of the given {@link Integer}.
+ *
+ * @param value
+ * the input value
+ *
+ * @return the raw {@link String} value that correspond to it
+ */
+ static public String fromInteger(int value) {
+ return Integer.toString(value);
+ }
+
+ /**
+ * Return a {@link String} representation of the given {@link Integer}.
+ *
+ * @param value
+ * the input value
+ *
+ * @return the raw {@link String} value that correspond to it
+ */
+ static public String fromBoolean(int value) {
+ return Integer.toString(value);
+ }
+
+ /**
+ * Convert the given {@link String} into a {@link Character} if it
+ * represents a {@link Character}, or NULL if it doesn't.
+ * <p>
+ * Note: null, "strange text", ""... will all be converted to NULL
+ * (remember: any {@link String} whose length is not 1 is <b>not</b> a
+ * {@link Character}).
+ *
+ * @param str
+ * the input {@link String}
+ *
+ * @return the converted {@link Character} or NULL
+ */
+ static public Character parseCharacter(String str) {
+ String s = str.trim();
+ if (s.length() == 1) {
+ return s.charAt(0);
+ }
+
+ return null;
+ }
+
+ /**
+ * Return a {@link String} representation of the given {@link Boolean}.
+ *
+ * @param value
+ * the input value
+ *
+ * @return the raw {@link String} value that correspond to it
+ */
+ static public String fromCharacter(char value) {
+ return Character.toString(value);
+ }
+
+ /**
+ * Convert the given {@link String} into a colour (represented here as an
+ * {@link Integer}) if it represents a colour, or NULL if it doesn't.
+ * <p>
+ * The returned colour value is an ARGB value.
+ *
+ * @param str
+ * the input {@link String}
+ *
+ * @return the converted colour as an {@link Integer} value or NULL
+ */
+ static Integer parseColor(String str) {
+ Integer rep = null;
+
+ str = str.trim();
+ int r = 0, g = 0, b = 0, a = -1;
+ if (str.startsWith("#") && (str.length() == 7 || str.length() == 9)) {
+ try {
+ r = Integer.parseInt(str.substring(1, 3), 16);
+ g = Integer.parseInt(str.substring(3, 5), 16);
+ b = Integer.parseInt(str.substring(5, 7), 16);
+ if (str.length() == 9) {
+ a = Integer.parseInt(str.substring(7, 9), 16);
+ } else {
+ a = 255;
+ }
+
+ } catch (NumberFormatException e) {
+ // no changes
+ }
+ }
+
+ // Try by name if still not found
+ if (a == -1) {
+ if ("black".equalsIgnoreCase(str)) {
+ a = 255;
+ r = 0;
+ g = 0;
+ b = 0;
+ } else if ("white".equalsIgnoreCase(str)) {
+ a = 255;
+ r = 255;
+ g = 255;
+ b = 255;
+ } else if ("red".equalsIgnoreCase(str)) {
+ a = 255;
+ r = 255;
+ g = 0;
+ b = 0;
+ } else if ("green".equalsIgnoreCase(str)) {
+ a = 255;
+ r = 0;
+ g = 255;
+ b = 0;
+ } else if ("blue".equalsIgnoreCase(str)) {
+ a = 255;
+ r = 0;
+ g = 0;
+ b = 255;
+ } else if ("grey".equalsIgnoreCase(str)
+ || "gray".equalsIgnoreCase(str)) {
+ a = 255;
+ r = 128;
+ g = 128;
+ b = 128;
+ } else if ("cyan".equalsIgnoreCase(str)) {
+ a = 255;
+ r = 0;
+ g = 255;
+ b = 255;
+ } else if ("magenta".equalsIgnoreCase(str)) {
+ a = 255;
+ r = 255;
+ g = 0;
+ b = 255;
+ } else if ("yellow".equalsIgnoreCase(str)) {
+ a = 255;
+ r = 255;
+ g = 255;
+ b = 0;
+ }
+ }
+
+ if (a != -1) {
+ rep = ((a & 0xFF) << 24) //
+ | ((r & 0xFF) << 16) //
+ | ((g & 0xFF) << 8) //
+ | ((b & 0xFF) << 0);
+ }
+
+ return rep;
+ }
+
+ /**
+ * Return a {@link String} representation of the given colour.
+ * <p>
+ * The colour value is interpreted as an ARGB value.
+ *
+ * @param color
+ * the ARGB colour value
+ * @return the raw {@link String} value that correspond to it
+ */
+ static public String fromColour(int color) {
+ int a = (color >> 24) & 0xFF;
+ int r = (color >> 16) & 0xFF;
+ int g = (color >> 8) & 0xFF;
+ int b = (color >> 0) & 0xFF;
+
+ String rs = Integer.toString(r, 16);
+ String gs = Integer.toString(g, 16);
+ String bs = Integer.toString(b, 16);
+ String as = "";
+ if (a < 255) {
+ as = Integer.toString(a, 16);
+ }
+
+ return "#" + rs + gs + bs + as;
+ }
+
+ /**
+ * Return a {@link String} representation of the given list of values.
+ * <p>
+ * The list of values is comma-separated and each value is surrounded by
+ * double-quotes; backslashes and double-quotes are escaped by a backslash.
+ *
+ * @param str
+ * the input value
+ * @return the raw {@link String} value that correspond to it
+ */
+ static public List<String> parseList(String str) {
+ if (str == null) {
+ return null;
+ }
+ List<String> list = new ArrayList<String>();
+ try {
+ boolean inQuote = false;
+ boolean prevIsBackSlash = false;
+ StringBuilder builder = new StringBuilder();
+ for (int i = 0; i < str.length(); i++) {
+ char car = str.charAt(i);
+
+ if (prevIsBackSlash) {
+ builder.append(car);
+ prevIsBackSlash = false;
+ } else {
+ switch (car) {
+ case '"':
+ if (inQuote) {
+ list.add(builder.toString());
+ builder.setLength(0);
+ }
+
+ inQuote = !inQuote;
+ break;
+ case '\\':
+ prevIsBackSlash = true;
+ break;
+ case ' ':
+ case '\n':
+ case '\r':
+ if (inQuote) {
+ builder.append(car);
+ }
+ break;
+
+ case ',':
+ if (!inQuote) {
+ break;
+ }
+ // continue to default
+ default:
+ if (!inQuote) {
+ // Bad format!
+ return null;
+ }
+
+ builder.append(car);
+ break;
+ }
+ }
+ }
+
+ if (inQuote || prevIsBackSlash) {
+ // Bad format!
+ return null;
+ }
+
+ } catch (Exception e) {
+ return null;
+ }
+
+ return list;
+ }
+
+ /**
+ * Return a {@link String} representation of the given list of values.
+ *
+ * @param list
+ * the input value
+ *
+ * @return the raw {@link String} value that correspond to it
+ */
+ static public String fromList(List<String> list) {
+ StringBuilder builder = new StringBuilder();
+ for (String item : list) {
+ if (builder.length() > 0) {
+ builder.append(", ");
+ }
+ builder.append('"')//
+ .append(item.replace("\\", "\\\\").replace("\"", "\\\""))//
+ .append('"');
+ }
+
+ return builder.toString();
+ }
+}