1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
package net.admin4j.util; |
15 | |
|
16 | |
import java.text.DecimalFormat; |
17 | |
import java.util.Date; |
18 | |
|
19 | |
import net.admin4j.deps.commons.lang3.StringEscapeUtils; |
20 | |
import net.admin4j.deps.commons.lang3.StringUtils; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | 81 | public class GuiUtils { |
28 | |
|
29 | |
public static String htmlEscape(Object obj) { |
30 | 15 | String str = null; |
31 | 15 | if (obj instanceof String) { |
32 | 9 | str = (String)obj; |
33 | |
} |
34 | 6 | else if (obj == null) { |
35 | 3 | str = ""; |
36 | |
} |
37 | 3 | else str = obj.toString(); |
38 | |
|
39 | |
return StringEscapeUtils.escapeHtml4(str); |
40 | |
} |
41 | |
|
42 | |
public static String javascriptEscape(Object obj) { |
43 | 12 | String str = null; |
44 | 12 | if (obj instanceof String) { |
45 | 6 | str = (String)obj; |
46 | |
} |
47 | 6 | else if (obj == null) { |
48 | 3 | str = ""; |
49 | |
} |
50 | 3 | else str = obj.toString(); |
51 | |
|
52 | |
str = StringUtils.replace(str, "'", "\\'"); |
53 | |
str = StringUtils.replace(str, "\"", "\\\""); |
54 | |
|
55 | 12 | return str; |
56 | |
} |
57 | |
|
58 | |
public static String objectId(Object obj) { |
59 | 6 | if (obj == null) return "-1"; |
60 | 3 | DecimalFormat format = new DecimalFormat("#"); |
61 | 3 | return format.format(System.identityHashCode(obj)); |
62 | |
} |
63 | |
|
64 | |
public static Date toDate(Object timeInMillis) { |
65 | 141 | if (timeInMillis == null) return null; |
66 | 138 | if (timeInMillis instanceof Number) { |
67 | 138 | return new Date( ((Number)timeInMillis).longValue()); |
68 | |
} |
69 | |
|
70 | 0 | throw new Admin4jRuntimeException("Invalid Long date") |
71 | |
.addContextValue("timeInMillis", timeInMillis); |
72 | |
} |
73 | |
|
74 | |
public static Double bytes2Mb(Number bytes) { |
75 | 156 | if (bytes == null) { |
76 | 3 | return null; |
77 | |
} |
78 | |
|
79 | 153 | return bytes.doubleValue() / 1024000.000; |
80 | |
} |
81 | |
|
82 | |
public static String abbreviate(String text, Number maxNbrChars) { |
83 | 6 | return abbreviate(text, maxNbrChars, "..."); |
84 | |
} |
85 | |
|
86 | |
public static String abbreviate(String text, Number maxNbrChars, String suffix) { |
87 | 9 | if (text == null) { |
88 | 3 | return text; |
89 | |
} |
90 | 6 | String localSuffix = suffix; |
91 | 6 | if (localSuffix == null) { |
92 | 0 | localSuffix = ""; |
93 | |
} |
94 | |
|
95 | 6 | if (text.length() > maxNbrChars.intValue()) { |
96 | 6 | return text.substring(0, maxNbrChars.intValue()) + localSuffix; |
97 | |
} |
98 | 0 | return text; |
99 | |
} |
100 | |
|
101 | |
} |