Merge branch 'subtree'
[fanfix.git] / src / be / nikiroo / utils / resources / BundleHelper.java
index 94066d8685572a6027a88d88f19a0d31a8175b31..c6b26c71985048cfb94fa5cdbf1f7f027b9f9bb8 100644 (file)
@@ -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();
@@ -227,7 +244,7 @@ class BundleHelper {
         *            the ARGB colour value
         * @return the raw {@link String} value that correspond to it
         */
-       static public String fromColour(int color) {
+       static public String fromColor(int color) {
                int a = (color >> 24) & 0xFF;
                int r = (color >> 16) & 0xFF;
                int g = (color >> 8) & 0xFF;
@@ -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<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;
@@ -267,19 +315,25 @@ class BundleHelper {
                                char car = str.charAt(i);
 
                                if (prevIsBackSlash) {
+                                       // We don't process it here
                                        builder.append(car);
                                        prevIsBackSlash = false;
                                } else {
                                        switch (car) {
                                        case '"':
+                                               // We don't process it here
+                                               builder.append(car);
+
                                                if (inQuote) {
-                                                       list.add(builder.toString());
+                                                       list.add(unescape(builder.toString()));
                                                        builder.setLength(0);
                                                }
 
                                                inQuote = !inQuote;
                                                break;
-                                       case '\\':
+                                       case '^':
+                                               // We don't process it here
+                                               builder.append(car);
                                                prevIsBackSlash = true;
                                                break;
                                        case ' ':
@@ -321,6 +375,18 @@ class BundleHelper {
 
        /**
         * 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
@@ -328,16 +394,196 @@ class BundleHelper {
         * @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(", ");
                        }
-                       builder.append('"')//
-                                       .append(item.replace("\\", "\\\\").replace("\"", "\\\""))//
-                                       .append('"');
+                       builder.append(escape(item));
+               }
+
+               return builder.toString();
+       }
+
+       /**
+        * 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)}
+        * 
+        * @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;
+       }
 }