X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fresources%2FBundleHelper.java;h=c6b26c71985048cfb94fa5cdbf1f7f027b9f9bb8;hb=abfeadcc2c0da88e32dd49291e8163880fe270f1;hp=0d48d67a0d031b8b4902e5536fd696b965fdf8b7;hpb=b15a49851835d45d35ba9fdd28521df34021828f;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/resources/BundleHelper.java b/src/be/nikiroo/utils/resources/BundleHelper.java index 0d48d67..c6b26c7 100644 --- a/src/be/nikiroo/utils/resources/BundleHelper.java +++ b/src/be/nikiroo/utils/resources/BundleHelper.java @@ -18,20 +18,25 @@ class BundleHelper { * * @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; } @@ -55,10 +60,18 @@ class BundleHelper { * * @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) { @@ -79,18 +92,6 @@ class BundleHelper { 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. @@ -101,10 +102,18 @@ class BundleHelper { * * @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); @@ -133,10 +142,18 @@ class BundleHelper { * * @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(); @@ -244,20 +261,51 @@ class BundleHelper { 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 list = parseList(raw, -1); + if (list == null) { + return -1; + } + + return list.size(); + } + /** * Return a {@link String} representation of the given list of values. *

* 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 parseList(String str) { + static public List parseList(String str, int item) { if (str == null) { return null; } + + if (item >= 0) { + str = getItem(str, item); + } + List list = new ArrayList(); try { boolean inQuote = false; @@ -283,7 +331,7 @@ class BundleHelper { inQuote = !inQuote; break; - case '\\': + case '^': // We don't process it here builder.append(car); prevIsBackSlash = true; @@ -327,6 +375,18 @@ class BundleHelper { /** * Return a {@link String} representation of the given list of values. + *

+ * NULL will be assimilated to an empty {@link String} if later non-null + * values exist, or just ignored if not. + *

+ * Example: + *

* * @param list * the input value @@ -334,8 +394,24 @@ class BundleHelper { * @return the raw {@link String} value that correspond to it */ static public String fromList(List list) { + if (list == null) { + list = new ArrayList(); + } + + 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(", "); } @@ -346,7 +422,72 @@ class BundleHelper { } /** - * Escape the given value for list formating (no \\, no \n). + * Return a {@link String} representation of the given list of values. + *

+ * NULL will be assimilated to an empty {@link String} if later non-null + * values exist, or just ignored if not. + *

+ * Example: + *

    + *
  • 1,NULL, 3 will become 1, + * "", 3
  • + *
  • 1,NULL, NULL will become 1
  • + *
  • NULL, NULL, NULL will become an empty list + *
  • + *
+ * + * @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 list, String value, int item) { + if (list == null) { + list = new ArrayList(); + } + + 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. + *

+ * NULL will be assimilated to an empty {@link String} if later non-null + * values exist, or just ignored if not. + *

+ * Example: + *

    + *
  • 1,NULL, 3 will become 1, + * "", 3
  • + *
  • 1,NULL, NULL will become 1
  • + *
  • NULL, NULL, NULL will become an empty list + *
  • + *
+ * + * @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...). *

* You can unescape it with {@link BundleHelper#unescape(String)} * @@ -358,16 +499,16 @@ class BundleHelper { */ 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). *

* You can escape it with {@link BundleHelper#escape(String)} * @@ -385,7 +526,7 @@ class BundleHelper { return null; } - value = value.substring(1, value.length() - 2); + value = value.substring(1, value.length() - 1); boolean prevIsBackslash = false; StringBuilder builder = new StringBuilder(); @@ -400,12 +541,13 @@ class BundleHelper { 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); @@ -420,4 +562,28 @@ class BundleHelper { 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 item if found, + * NULL if not + */ + static private String getItem(String value, int item) { + if (item >= 0) { + value = null; + List values = parseList(value, -1); + if (values != null && item < values.size()) { + value = values.get(item); + } + } + + return value; + } }