X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fresources%2FBundleHelper.java;h=0d48d67a0d031b8b4902e5536fd696b965fdf8b7;hb=fde375c1fc9b2fe29b556f8d2bd573c6b8b480dc;hp=94066d8685572a6027a88d88f19a0d31a8175b31;hpb=3bfc1d20054f0a97f268f1fffea29df10ae14b42;p=fanfix.git diff --git a/src/be/nikiroo/utils/resources/BundleHelper.java b/src/be/nikiroo/utils/resources/BundleHelper.java index 94066d8..0d48d67 100644 --- a/src/be/nikiroo/utils/resources/BundleHelper.java +++ b/src/be/nikiroo/utils/resources/BundleHelper.java @@ -227,7 +227,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; @@ -267,19 +267,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 '\\': + // We don't process it here + builder.append(car); prevIsBackSlash = true; break; case ' ': @@ -333,9 +339,83 @@ class BundleHelper { if (builder.length() > 0) { builder.append(", "); } - builder.append('"')// - .append(item.replace("\\", "\\\\").replace("\"", "\\\""))// - .append('"'); + builder.append(escape(item)); + } + + return builder.toString(); + } + + /** + * Escape the given value for list formating (no \\, no \n). + *

+ * 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 \n and so + * on). + *

+ * 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() - 2); + + 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; + } + } else { + if (car == '\\') { + prevIsBackslash = true; + } else { + builder.append(car); + } + } + } + + if (prevIsBackslash) { + // Bad format + return null; } return builder.toString();