+
+ /**
+ * Escape the given value for list formating (no carets, no NEWLINES...).
+ * <p>
+ * You can unescape it with {@link BundleHelper#unescape(String)}
+ *
+ * @param value
+ * the value to escape
+ *
+ * @return an escaped value that can unquoted by the reverse operation
+ * {@link BundleHelper#unescape(String)}
+ */
+ static public String escape(String value) {
+ return '"' + value//
+ .replace("^", "^^") //
+ .replace("\"", "^\"") //
+ .replace("\n", "^\n") //
+ .replace("\r", "^\r") //
+ + '"';
+ }
+
+ /**
+ * Unescape the given value for list formating (change ^n into NEWLINE and
+ * so on).
+ * <p>
+ * You can escape it with {@link BundleHelper#escape(String)}
+ *
+ * @param value
+ * the value to escape
+ *
+ * @return an unescaped value that can reverted by the reverse operation
+ * {@link BundleHelper#escape(String)}, or NULL if it was badly
+ * formated
+ */
+ static public String unescape(String value) {
+ if (value.length() < 2 || !value.startsWith("\"")
+ || !value.endsWith("\"")) {
+ // Bad format
+ return null;
+ }
+
+ value = value.substring(1, value.length() - 1);
+
+ boolean prevIsBackslash = false;
+ StringBuilder builder = new StringBuilder();
+ for (char car : value.toCharArray()) {
+ if (prevIsBackslash) {
+ switch (car) {
+ case 'n':
+ case 'N':
+ builder.append('\n');
+ break;
+ case 'r':
+ case 'R':
+ builder.append('\r');
+ break;
+ default: // includes ^ and "
+ builder.append(car);
+ break;
+ }
+ prevIsBackslash = false;
+ } else {
+ if (car == '^') {
+ prevIsBackslash = true;
+ } else {
+ builder.append(car);
+ }
+ }
+ }
+
+ if (prevIsBackslash) {
+ // Bad format
+ return null;
+ }
+
+ 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;
+ }