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.
270 * @return its size if it is a list, -1 if not
272 static public int getListSize(String raw
) {
273 List
<String
> list
= parseList(raw
, -1);
282 * Return a {@link String} representation of the given list of values.
284 * The list of values is comma-separated and each value is surrounded by
285 * double-quotes; caret (^) and double-quotes (") are escaped by a caret.
290 * the item number to use for an array of values, or -1 for
293 * @return the raw {@link String} value that correspond to it
295 static public List
<String
> parseList(String str
, int item
) {
301 str
= getItem(str
, item
);
304 List
<String
> list
= new ArrayList
<String
>();
306 boolean inQuote
= false;
307 boolean prevIsBackSlash
= false;
308 StringBuilder builder
= new StringBuilder();
309 for (int i
= 0; i
< str
.length(); i
++) {
310 char car
= str
.charAt(i
);
312 if (prevIsBackSlash
) {
313 // We don't process it here
315 prevIsBackSlash
= false;
319 // We don't process it here
323 list
.add(unescape(builder
.toString()));
324 builder
.setLength(0);
330 // We don't process it here
332 prevIsBackSlash
= true;
346 // continue to default
359 if (inQuote
|| prevIsBackSlash
) {
364 } catch (Exception e
) {
372 * Return a {@link String} representation of the given list of values.
374 * NULL will be assimilated to an empty {@link String}.
379 * @return the raw {@link String} value that correspond to it
381 static public String
fromList(List
<String
> list
) {
382 StringBuilder builder
= new StringBuilder();
383 for (String item
: list
) {
384 if (builder
.length() > 0) {
385 builder
.append(", ");
387 builder
.append(escape(item
== null ?
"" : item
));
390 return builder
.toString();
394 * Escape the given value for list formating (no carets, no NEWLINES...).
396 * You can unescape it with {@link BundleHelper#unescape(String)}
399 * the value to escape
401 * @return an escaped value that can unquoted by the reverse operation
402 * {@link BundleHelper#unescape(String)}
404 static public String
escape(String value
) {
406 .replace("^", "^^") //
407 .replace("\"", "^\"") //
408 .replace("\n", "^\n") //
409 .replace("\r", "^\r") //
414 * Unescape the given value for list formating (change ^n into NEWLINE and
417 * You can escape it with {@link BundleHelper#escape(String)}
420 * the value to escape
422 * @return an unescaped value that can reverted by the reverse operation
423 * {@link BundleHelper#escape(String)}, or NULL if it was badly
426 static public String
unescape(String value
) {
427 if (value
.length() < 2 || !value
.startsWith("\"")
428 || !value
.endsWith("\"")) {
433 value
= value
.substring(1, value
.length() - 1);
435 boolean prevIsBackslash
= false;
436 StringBuilder builder
= new StringBuilder();
437 for (char car
: value
.toCharArray()) {
438 if (prevIsBackslash
) {
442 builder
.append('\n');
446 builder
.append('\r');
448 default: // includes ^ and "
452 prevIsBackslash
= false;
455 prevIsBackslash
= true;
462 if (prevIsBackslash
) {
467 return builder
.toString();
471 * Retrieve the specific item in the given value, assuming it is an array.
474 * the value to look into
476 * the item number to get for an array of values, or -1 for
477 * non-arrays (in that case, simply return the value as-is)
479 * @return the value as-is for non arrays, the item <tt>item</tt> if found,
482 static private String
getItem(String value
, int item
) {
485 List
<String
> values
= parseList(value
, -1);
486 if (values
!= null && item
< values
.size()) {
487 value
= values
.get(item
);