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 * @return the converted {@link Boolean} or NULL
24 static public Boolean
parseBoolean(String str
) {
25 if (str
!= null && str
.length() > 0) {
26 if (str
.equalsIgnoreCase("true") || str
.equalsIgnoreCase("on")
27 || str
.equalsIgnoreCase("yes"))
29 if (str
.equalsIgnoreCase("false") || str
.equalsIgnoreCase("off")
30 || str
.equalsIgnoreCase("no"))
39 * Return a {@link String} representation of the given {@link Boolean}.
44 * @return the raw {@link String} value that correspond to it
46 static public String
fromBoolean(boolean value
) {
47 return Boolean
.toString(value
);
51 * Convert the given {@link String} into a {@link Integer} if it represents
52 * a {@link Integer}, or NULL if it doesn't.
54 * Note: null, "strange text", ""... will all be converted to NULL.
57 * the input {@link String}
59 * @return the converted {@link Integer} or NULL
61 static public Integer
parseInteger(String str
) {
63 return Integer
.parseInt(str
);
64 } catch (Exception e
) {
71 * Return a {@link String} representation of the given {@link Integer}.
76 * @return the raw {@link String} value that correspond to it
78 static public String
fromInteger(int value
) {
79 return Integer
.toString(value
);
83 * Return a {@link String} representation of the given {@link Integer}.
88 * @return the raw {@link String} value that correspond to it
90 static public String
fromBoolean(int value
) {
91 return Integer
.toString(value
);
95 * Convert the given {@link String} into a {@link Character} if it
96 * represents a {@link Character}, or NULL if it doesn't.
98 * Note: null, "strange text", ""... will all be converted to NULL
99 * (remember: any {@link String} whose length is not 1 is <b>not</b> a
100 * {@link Character}).
103 * the input {@link String}
105 * @return the converted {@link Character} or NULL
107 static public Character
parseCharacter(String str
) {
108 String s
= str
.trim();
109 if (s
.length() == 1) {
117 * Return a {@link String} representation of the given {@link Boolean}.
122 * @return the raw {@link String} value that correspond to it
124 static public String
fromCharacter(char value
) {
125 return Character
.toString(value
);
129 * Convert the given {@link String} into a colour (represented here as an
130 * {@link Integer}) if it represents a colour, or NULL if it doesn't.
132 * The returned colour value is an ARGB value.
135 * the input {@link String}
137 * @return the converted colour as an {@link Integer} value or NULL
139 static Integer
parseColor(String str
) {
143 int r
= 0, g
= 0, b
= 0, a
= -1;
144 if (str
.startsWith("#") && (str
.length() == 7 || str
.length() == 9)) {
146 r
= Integer
.parseInt(str
.substring(1, 3), 16);
147 g
= Integer
.parseInt(str
.substring(3, 5), 16);
148 b
= Integer
.parseInt(str
.substring(5, 7), 16);
149 if (str
.length() == 9) {
150 a
= Integer
.parseInt(str
.substring(7, 9), 16);
155 } catch (NumberFormatException e
) {
160 // Try by name if still not found
162 if ("black".equalsIgnoreCase(str
)) {
167 } else if ("white".equalsIgnoreCase(str
)) {
172 } else if ("red".equalsIgnoreCase(str
)) {
177 } else if ("green".equalsIgnoreCase(str
)) {
182 } else if ("blue".equalsIgnoreCase(str
)) {
187 } else if ("grey".equalsIgnoreCase(str
)
188 || "gray".equalsIgnoreCase(str
)) {
193 } else if ("cyan".equalsIgnoreCase(str
)) {
198 } else if ("magenta".equalsIgnoreCase(str
)) {
203 } else if ("yellow".equalsIgnoreCase(str
)) {
212 rep
= ((a
& 0xFF) << 24) //
213 | ((r
& 0xFF) << 16) //
214 | ((g
& 0xFF) << 8) //
222 * Return a {@link String} representation of the given colour.
224 * The colour value is interpreted as an ARGB value.
227 * the ARGB colour value
228 * @return the raw {@link String} value that correspond to it
230 static public String
fromColor(int color
) {
231 int a
= (color
>> 24) & 0xFF;
232 int r
= (color
>> 16) & 0xFF;
233 int g
= (color
>> 8) & 0xFF;
234 int b
= (color
>> 0) & 0xFF;
236 String rs
= Integer
.toString(r
, 16);
237 String gs
= Integer
.toString(g
, 16);
238 String bs
= Integer
.toString(b
, 16);
241 as
= Integer
.toString(a
, 16);
244 return "#" + rs
+ gs
+ bs
+ as
;
248 * Return a {@link String} representation of the given list of values.
250 * The list of values is comma-separated and each value is surrounded by
251 * double-quotes; backslashes and double-quotes are escaped by a backslash.
255 * @return the raw {@link String} value that correspond to it
257 static public List
<String
> parseList(String str
) {
261 List
<String
> list
= new ArrayList
<String
>();
263 boolean inQuote
= false;
264 boolean prevIsBackSlash
= false;
265 StringBuilder builder
= new StringBuilder();
266 for (int i
= 0; i
< str
.length(); i
++) {
267 char car
= str
.charAt(i
);
269 if (prevIsBackSlash
) {
270 // We don't process it here
272 prevIsBackSlash
= false;
276 // We don't process it here
280 list
.add(unescape(builder
.toString()));
281 builder
.setLength(0);
287 // We don't process it here
289 prevIsBackSlash
= true;
303 // continue to default
316 if (inQuote
|| prevIsBackSlash
) {
321 } catch (Exception e
) {
329 * Return a {@link String} representation of the given list of values.
334 * @return the raw {@link String} value that correspond to it
336 static public String
fromList(List
<String
> list
) {
337 StringBuilder builder
= new StringBuilder();
338 for (String item
: list
) {
339 if (builder
.length() > 0) {
340 builder
.append(", ");
342 builder
.append(escape(item
));
345 return builder
.toString();
349 * Escape the given value for list formating (no \\, no \n).
351 * You can unescape it with {@link BundleHelper#unescape(String)}
354 * the value to escape
356 * @return an escaped value that can unquoted by the reverse operation
357 * {@link BundleHelper#unescape(String)}
359 static public String
escape(String value
) {
361 .replace("\\", "\\\\") //
362 .replace("\"", "\\\"") //
363 .replace("\n", "\\\n") //
364 .replace("\r", "\\\r") //
369 * Unescape the given value for list formating (change \\n into \n and so
372 * You can escape it with {@link BundleHelper#escape(String)}
375 * the value to escape
377 * @return an unescaped value that can reverted by the reverse operation
378 * {@link BundleHelper#escape(String)}, or NULL if it was badly
381 static public String
unescape(String value
) {
382 if (value
.length() < 2 || !value
.startsWith("\"")
383 || !value
.endsWith("\"")) {
388 value
= value
.substring(1, value
.length() - 1);
390 boolean prevIsBackslash
= false;
391 StringBuilder builder
= new StringBuilder();
392 for (char car
: value
.toCharArray()) {
393 if (prevIsBackslash
) {
397 builder
.append('\n');
401 builder
.append('\r');
403 default: // includes \ and "
409 prevIsBackslash
= true;
416 if (prevIsBackslash
) {
421 return builder
.toString();