1 package be
.nikiroo
.utils
.resources
;
3 import java
.util
.ArrayList
;
7 * Internal class used to convert data to/from {@link String}s in the context of
14 * Convert the given {@link String} into a {@link Boolean} if it represents
15 * a {@link Boolean}, or NULL if it doesn't.
17 * Note: null, "strange text", ""... will all be converted to NULL.
20 * the input {@link String}
22 * the item number to use for an array of values, or -1 for
25 * @return the converted {@link Boolean} or NULL
27 static public Boolean
parseBoolean(String str
, int item
) {
28 str
= getItem(str
, item
);
33 if (str
.equalsIgnoreCase("true") || str
.equalsIgnoreCase("on")
34 || str
.equalsIgnoreCase("yes"))
36 if (str
.equalsIgnoreCase("false") || str
.equalsIgnoreCase("off")
37 || str
.equalsIgnoreCase("no"))
44 * Return a {@link String} representation of the given {@link Boolean}.
49 * @return the raw {@link String} value that correspond to it
51 static public String
fromBoolean(boolean value
) {
52 return Boolean
.toString(value
);
56 * Convert the given {@link String} into a {@link Integer} if it represents
57 * a {@link Integer}, or NULL if it doesn't.
59 * Note: null, "strange text", ""... will all be converted to NULL.
62 * the input {@link String}
64 * the item number to use for an array of values, or -1 for
67 * @return the converted {@link Integer} or NULL
69 static public Integer
parseInteger(String str
, int item
) {
70 str
= getItem(str
, item
);
76 return Integer
.parseInt(str
);
77 } catch (Exception e
) {
84 * Return a {@link String} representation of the given {@link Integer}.
89 * @return the raw {@link String} value that correspond to it
91 static public String
fromInteger(int value
) {
92 return Integer
.toString(value
);
96 * Convert the given {@link String} into a {@link Character} if it
97 * represents a {@link Character}, or NULL if it doesn't.
99 * Note: null, "strange text", ""... will all be converted to NULL
100 * (remember: any {@link String} whose length is not 1 is <b>not</b> a
101 * {@link Character}).
104 * the input {@link String}
106 * the item number to use for an array of values, or -1 for
109 * @return the converted {@link Character} or NULL
111 static public Character
parseCharacter(String str
, int item
) {
112 str
= getItem(str
, item
);
117 String s
= str
.trim();
118 if (s
.length() == 1) {
126 * Return a {@link String} representation of the given {@link Boolean}.
131 * @return the raw {@link String} value that correspond to it
133 static public String
fromCharacter(char value
) {
134 return Character
.toString(value
);
138 * Convert the given {@link String} into a colour (represented here as an
139 * {@link Integer}) if it represents a colour, or NULL if it doesn't.
141 * The returned colour value is an ARGB value.
144 * the input {@link String}
146 * the item number to use for an array of values, or -1 for
149 * @return the converted colour as an {@link Integer} value or NULL
151 static Integer
parseColor(String str
, int item
) {
152 str
= getItem(str
, item
);
160 int r
= 0, g
= 0, b
= 0, a
= -1;
161 if (str
.startsWith("#") && (str
.length() == 7 || str
.length() == 9)) {
163 r
= Integer
.parseInt(str
.substring(1, 3), 16);
164 g
= Integer
.parseInt(str
.substring(3, 5), 16);
165 b
= Integer
.parseInt(str
.substring(5, 7), 16);
166 if (str
.length() == 9) {
167 a
= Integer
.parseInt(str
.substring(7, 9), 16);
172 } catch (NumberFormatException e
) {
177 // Try by name if still not found
179 if ("black".equalsIgnoreCase(str
)) {
184 } else if ("white".equalsIgnoreCase(str
)) {
189 } else if ("red".equalsIgnoreCase(str
)) {
194 } else if ("green".equalsIgnoreCase(str
)) {
199 } else if ("blue".equalsIgnoreCase(str
)) {
204 } else if ("grey".equalsIgnoreCase(str
)
205 || "gray".equalsIgnoreCase(str
)) {
210 } else if ("cyan".equalsIgnoreCase(str
)) {
215 } else if ("magenta".equalsIgnoreCase(str
)) {
220 } else if ("yellow".equalsIgnoreCase(str
)) {
229 rep
= ((a
& 0xFF) << 24) //
230 | ((r
& 0xFF) << 16) //
231 | ((g
& 0xFF) << 8) //
239 * Return a {@link String} representation of the given colour.
241 * The colour value is interpreted as an ARGB value.
244 * the ARGB colour value
245 * @return the raw {@link String} value that correspond to it
247 static public String
fromColor(int color
) {
248 int a
= (color
>> 24) & 0xFF;
249 int r
= (color
>> 16) & 0xFF;
250 int g
= (color
>> 8) & 0xFF;
251 int b
= (color
>> 0) & 0xFF;
253 String rs
= Integer
.toString(r
, 16);
254 String gs
= Integer
.toString(g
, 16);
255 String bs
= Integer
.toString(b
, 16);
258 as
= Integer
.toString(a
, 16);
261 return "#" + rs
+ gs
+ bs
+ as
;
265 * The size of this raw list (note than a NULL list is of size 0).
270 * @return its size if it is a list (NULL is an empty list), -1 if it is not
273 static public int getListSize(String raw
) {
278 List
<String
> list
= parseList(raw
, -1);
287 * Return a {@link String} representation of the given list of values.
289 * The list of values is comma-separated and each value is surrounded by
290 * double-quotes; caret (^) and double-quotes (") are escaped by a caret.
295 * the item number to use for an array of values, or -1 for
298 * @return the raw {@link String} value that correspond to it
300 static public List
<String
> parseList(String str
, int item
) {
306 str
= getItem(str
, item
);
309 List
<String
> list
= new ArrayList
<String
>();
311 boolean inQuote
= false;
312 boolean prevIsBackSlash
= false;
313 StringBuilder builder
= new StringBuilder();
314 for (int i
= 0; i
< str
.length(); i
++) {
315 char car
= str
.charAt(i
);
317 if (prevIsBackSlash
) {
318 // We don't process it here
320 prevIsBackSlash
= false;
324 // We don't process it here
328 list
.add(unescape(builder
.toString()));
329 builder
.setLength(0);
335 // We don't process it here
337 prevIsBackSlash
= true;
351 // continue to default
364 if (inQuote
|| prevIsBackSlash
) {
369 } catch (Exception e
) {
377 * Return a {@link String} representation of the given list of values.
379 * NULL will be assimilated to an empty {@link String} if later non-null
380 * values exist, or just ignored if not.
384 * <li><tt>1</tt>,<tt>NULL</tt>, <tt>3</tt> will become <tt>1</tt>,
385 * <tt>""</tt>, <tt>3</tt></li>
386 * <li><tt>1</tt>,<tt>NULL</tt>, <tt>NULL</tt> will become <tt>1</tt></li>
387 * <li><tt>NULL</tt>, <tt>NULL</tt>, <tt>NULL</tt> will become an empty list
394 * @return the raw {@link String} value that correspond to it
396 static public String
fromList(List
<String
> list
) {
398 list
= new ArrayList
<String
>();
401 int last
= list
.size() - 1;
402 for (int i
= 0; i
< list
.size(); i
++) {
403 if (list
.get(i
) != null) {
408 StringBuilder builder
= new StringBuilder();
409 for (int i
= 0; i
<= last
; i
++) {
410 String item
= list
.get(i
);
415 if (builder
.length() > 0) {
416 builder
.append(", ");
418 builder
.append(escape(item
));
421 return builder
.toString();
425 * Return a {@link String} representation of the given list of values.
427 * NULL will be assimilated to an empty {@link String} if later non-null
428 * values exist, or just ignored if not.
432 * <li><tt>1</tt>,<tt>NULL</tt>, <tt>3</tt> will become <tt>1</tt>,
433 * <tt>""</tt>, <tt>3</tt></li>
434 * <li><tt>1</tt>,<tt>NULL</tt>, <tt>NULL</tt> will become <tt>1</tt></li>
435 * <li><tt>NULL</tt>, <tt>NULL</tt>, <tt>NULL</tt> will become an empty list
442 * the value to insert
444 * the position to insert it at
446 * @return the raw {@link String} value that correspond to it
448 static public String
fromList(List
<String
> list
, String value
, int item
) {
450 list
= new ArrayList
<String
>();
453 while (item
>= list
.size()) {
456 list
.set(item
, value
);
458 return fromList(list
);
462 * Return a {@link String} representation of the given list of values.
464 * NULL will be assimilated to an empty {@link String} if later non-null
465 * values exist, or just ignored if not.
469 * <li><tt>1</tt>,<tt>NULL</tt>, <tt>3</tt> will become <tt>1</tt>,
470 * <tt>""</tt>, <tt>3</tt></li>
471 * <li><tt>1</tt>,<tt>NULL</tt>, <tt>NULL</tt> will become <tt>1</tt></li>
472 * <li><tt>NULL</tt>, <tt>NULL</tt>, <tt>NULL</tt> will become an empty list
479 * the value to insert
481 * the position to insert it at
483 * @return the raw {@link String} value that correspond to it
485 static public String
fromList(String list
, String value
, int item
) {
486 return fromList(parseList(list
, -1), value
, item
);
490 * Escape the given value for list formating (no carets, no NEWLINES...).
492 * You can unescape it with {@link BundleHelper#unescape(String)}
495 * the value to escape
497 * @return an escaped value that can unquoted by the reverse operation
498 * {@link BundleHelper#unescape(String)}
500 static public String
escape(String value
) {
502 .replace("^", "^^") //
503 .replace("\"", "^\"") //
504 .replace("\n", "^\n") //
505 .replace("\r", "^\r") //
510 * Unescape the given value for list formating (change ^n into NEWLINE and
513 * You can escape it with {@link BundleHelper#escape(String)}
516 * the value to escape
518 * @return an unescaped value that can reverted by the reverse operation
519 * {@link BundleHelper#escape(String)}, or NULL if it was badly
522 static public String
unescape(String value
) {
523 if (value
.length() < 2 || !value
.startsWith("\"")
524 || !value
.endsWith("\"")) {
529 value
= value
.substring(1, value
.length() - 1);
531 boolean prevIsBackslash
= false;
532 StringBuilder builder
= new StringBuilder();
533 for (char car
: value
.toCharArray()) {
534 if (prevIsBackslash
) {
538 builder
.append('\n');
542 builder
.append('\r');
544 default: // includes ^ and "
548 prevIsBackslash
= false;
551 prevIsBackslash
= true;
558 if (prevIsBackslash
) {
563 return builder
.toString();
567 * Retrieve the specific item in the given value, assuming it is an array.
570 * the value to look into
572 * the item number to get for an array of values, or -1 for
573 * non-arrays (in that case, simply return the value as-is)
575 * @return the value as-is for non arrays, the item <tt>item</tt> if found,
578 static private String
getItem(String value
, int item
) {
581 List
<String
> values
= parseList(value
, -1);
582 if (values
!= null && item
< values
.size()) {
583 value
= values
.get(item
);