*
* @param str
* the input {@link String}
+ * @param item
+ * the item number to use for an array of values, or -1 for
+ * non-arrays
*
* @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;
-
+ static public Boolean parseBoolean(String str, int item) {
+ str = getItem(str, item);
+ if (str == null) {
+ return null;
}
+ 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;
}
*
* @param str
* the input {@link String}
+ * @param item
+ * the item number to use for an array of values, or -1 for
+ * non-arrays
*
* @return the converted {@link Integer} or NULL
*/
- static public Integer parseInteger(String str) {
+ static public Integer parseInteger(String str, int item) {
+ str = getItem(str, item);
+ if (str == null) {
+ return null;
+ }
+
try {
return Integer.parseInt(str);
} catch (Exception e) {
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.
*
* @param str
* the input {@link String}
+ * @param item
+ * the item number to use for an array of values, or -1 for
+ * non-arrays
*
* @return the converted {@link Character} or NULL
*/
- static public Character parseCharacter(String str) {
+ static public Character parseCharacter(String str, int item) {
+ str = getItem(str, item);
+ if (str == null) {
+ return null;
+ }
+
String s = str.trim();
if (s.length() == 1) {
return s.charAt(0);
*
* @param str
* the input {@link String}
+ * @param item
+ * the item number to use for an array of values, or -1 for
+ * non-arrays
*
* @return the converted colour as an {@link Integer} value or NULL
*/
- static Integer parseColor(String str) {
+ static Integer parseColor(String str, int item) {
+ str = getItem(str, item);
+ if (str == null) {
+ return null;
+ }
+
Integer rep = null;
str = str.trim();
return "#" + rs + gs + bs + as;
}
+ /**
+ * The size of this raw list (note than a NULL list is of size 0).
+ *
+ * @param raw
+ * the raw list
+ *
+ * @return its size if it is a list (NULL is an empty list), -1 if it is not
+ * a list
+ */
+ static public int getListSize(String raw) {
+ if (raw == null) {
+ return 0;
+ }
+
+ List<String> list = parseList(raw, -1);
+ if (list == null) {
+ return -1;
+ }
+
+ return list.size();
+ }
+
/**
* 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.
+ * double-quotes; caret (^) and double-quotes (") are escaped by a caret.
*
* @param str
* the input value
+ * @param item
+ * the item number to use for an array of values, or -1 for
+ * non-arrays
+ *
* @return the raw {@link String} value that correspond to it
*/
- static public List<String> parseList(String str) {
+ static public List<String> parseList(String str, int item) {
if (str == null) {
return null;
}
+
+ if (item >= 0) {
+ str = getItem(str, item);
+ }
+
List<String> list = new ArrayList<String>();
try {
boolean inQuote = false;
inQuote = !inQuote;
break;
- case '\\':
+ case '^':
// We don't process it here
builder.append(car);
prevIsBackSlash = true;
/**
* Return a {@link String} representation of the given list of values.
+ * <p>
+ * NULL will be assimilated to an empty {@link String} if later non-null
+ * values exist, or just ignored if not.
+ * <p>
+ * Example:
+ * <ul>
+ * <li><tt>1</tt>,<tt>NULL</tt>, <tt>3</tt> will become <tt>1</tt>,
+ * <tt>""</tt>, <tt>3</tt></li>
+ * <li><tt>1</tt>,<tt>NULL</tt>, <tt>NULL</tt> will become <tt>1</tt></li>
+ * <li><tt>NULL</tt>, <tt>NULL</tt>, <tt>NULL</tt> will become an empty list
+ * </li>
+ * </ul>
*
* @param list
* the input value
* @return the raw {@link String} value that correspond to it
*/
static public String fromList(List<String> list) {
+ if (list == null) {
+ list = new ArrayList<String>();
+ }
+
+ int last = list.size() - 1;
+ for (int i = 0; i < list.size(); i++) {
+ if (list.get(i) != null) {
+ last = i;
+ }
+ }
+
StringBuilder builder = new StringBuilder();
- for (String item : list) {
+ for (int i = 0; i <= last; i++) {
+ String item = list.get(i);
+ if (item == null) {
+ item = "";
+ }
+
if (builder.length() > 0) {
builder.append(", ");
}
}
/**
- * Escape the given value for list formating (no \\, no \n).
+ * Return a {@link String} representation of the given list of values.
+ * <p>
+ * NULL will be assimilated to an empty {@link String} if later non-null
+ * values exist, or just ignored if not.
+ * <p>
+ * Example:
+ * <ul>
+ * <li><tt>1</tt>,<tt>NULL</tt>, <tt>3</tt> will become <tt>1</tt>,
+ * <tt>""</tt>, <tt>3</tt></li>
+ * <li><tt>1</tt>,<tt>NULL</tt>, <tt>NULL</tt> will become <tt>1</tt></li>
+ * <li><tt>NULL</tt>, <tt>NULL</tt>, <tt>NULL</tt> will become an empty list
+ * </li>
+ * </ul>
+ *
+ * @param list
+ * the input value
+ * @param value
+ * the value to insert
+ * @param item
+ * the position to insert it at
+ *
+ * @return the raw {@link String} value that correspond to it
+ */
+ static public String fromList(List<String> list, String value, int item) {
+ if (list == null) {
+ list = new ArrayList<String>();
+ }
+
+ while (item >= list.size()) {
+ list.add(null);
+ }
+ list.set(item, value);
+
+ return fromList(list);
+ }
+
+ /**
+ * Return a {@link String} representation of the given list of values.
+ * <p>
+ * NULL will be assimilated to an empty {@link String} if later non-null
+ * values exist, or just ignored if not.
+ * <p>
+ * Example:
+ * <ul>
+ * <li><tt>1</tt>,<tt>NULL</tt>, <tt>3</tt> will become <tt>1</tt>,
+ * <tt>""</tt>, <tt>3</tt></li>
+ * <li><tt>1</tt>,<tt>NULL</tt>, <tt>NULL</tt> will become <tt>1</tt></li>
+ * <li><tt>NULL</tt>, <tt>NULL</tt>, <tt>NULL</tt> will become an empty list
+ * </li>
+ * </ul>
+ *
+ * @param list
+ * the input value
+ * @param value
+ * the value to insert
+ * @param item
+ * the position to insert it at
+ *
+ * @return the raw {@link String} value that correspond to it
+ */
+ static public String fromList(String list, String value, int item) {
+ return fromList(parseList(list, -1), value, item);
+ }
+
+ /**
+ * Escape the given value for list formating (no carets, no NEWLINES...).
* <p>
* You can unescape it with {@link BundleHelper#unescape(String)}
*
*/
static public String escape(String value) {
return '"' + value//
- .replace("\\", "\\\\") //
- .replace("\"", "\\\"") //
- .replace("\n", "\\\n") //
- .replace("\r", "\\\r") //
+ .replace("^", "^^") //
+ .replace("\"", "^\"") //
+ .replace("\n", "^\n") //
+ .replace("\r", "^\r") //
+ '"';
}
/**
- * Unescape the given value for list formating (change \\n into \n and so
- * on).
+ * Unescape the given value for list formating (change ^n into NEWLINE and
+ * so on).
* <p>
* You can escape it with {@link BundleHelper#escape(String)}
*
return null;
}
- value = value.substring(1, value.length() - 2);
+ value = value.substring(1, value.length() - 1);
boolean prevIsBackslash = false;
StringBuilder builder = new StringBuilder();
case 'R':
builder.append('\r');
break;
- default: // includes \ and "
+ default: // includes ^ and "
builder.append(car);
break;
}
+ prevIsBackslash = false;
} else {
- if (car == '\\') {
+ if (car == '^') {
prevIsBackslash = true;
} else {
builder.append(car);
return builder.toString();
}
+
+ /**
+ * Retrieve the specific item in the given value, assuming it is an array.
+ *
+ * @param value
+ * the value to look into
+ * @param item
+ * the item number to get for an array of values, or -1 for
+ * non-arrays (in that case, simply return the value as-is)
+ *
+ * @return the value as-is for non arrays, the item <tt>item</tt> if found,
+ * NULL if not
+ */
+ static private String getItem(String value, int item) {
+ if (item >= 0) {
+ value = null;
+ List<String> values = parseList(value, -1);
+ if (values != null && item < values.size()) {
+ value = values.get(item);
+ }
+ }
+
+ return value;
+ }
}