1 package org.varienaja.util;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.net.MalformedURLException;
8 import java.net.URL;
9 import java.text.DecimalFormat;
10 import java.text.DecimalFormatSymbols;
11 import java.text.NumberFormat;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.Locale;
17 import java.util.Set;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20
21 import org.apache.commons.codec.binary.Hex;
22 import org.apache.commons.io.IOUtils;
23
24
25
26
27
28
29
30 public final class StringUtil {
31
32 public static final String ENCODING_LATIN = "ISO-8859-1";
33 public static final String ENCODING_UTF8 = "UTF-8";
34
35 private static final String[][] HTML_SUBSTITUTIONS = {
36 {"&", "&"},
37 {"<", "<"},
38 {">", ">"},
39 {"'", "'"},
40 {"\"", """},
41 };
42
43 private static final String[][] MIME_TYPES = {
44 {"mp3", "audio/mpeg"},
45 {"mpg", "video/mpeg"},
46 {"mpeg", "video/mpeg"},
47 {"mp4", "audio/mp4"},
48 {"m4a", "audio/mp4"},
49 {"mpg4", "audio/mp4"},
50 {"ogg", "application/ogg"},
51 };
52
53
54
55
56 private StringUtil() {}
57
58
59
60
61
62
63
64
65
66
67 public static String toHtml(String s) {
68 if (s == null) {
69 return null;
70 }
71 for (String[] substitution : HTML_SUBSTITUTIONS) {
72 if (s.contains(substitution[0])) {
73 s = s.replaceAll(substitution[0], substitution[1]);
74 }
75 }
76 return s;
77 }
78
79
80
81
82
83
84
85 public static String getSuffix(String s) {
86 int index = s.lastIndexOf('.');
87 return index == -1 ? "" : s.substring(index);
88 }
89
90
91
92
93
94
95
96 public static String removeSuffix(String s) {
97 int index = s.lastIndexOf('.');
98 return index == -1 ? s : s.substring(0, index);
99 }
100
101
102
103
104
105
106
107 public static String getMimeType(String suffix) {
108 for (String[] map : MIME_TYPES) {
109 if (map[0].equalsIgnoreCase(suffix) || ('.' + map[0]).equalsIgnoreCase(suffix)) {
110 return map[1];
111 }
112 }
113 return "application/octet-stream";
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 public static synchronized String formatBytes(long byteCount) {
130
131
132 if (byteCount >= 1024 * 1024 * 1024) {
133 NumberFormat gigaByteFormat = new DecimalFormat("0.00 GB", new DecimalFormatSymbols());
134 return gigaByteFormat.format((double) byteCount / (1024 * 1024 * 1024));
135 }
136
137
138 if (byteCount >= 1024 * 1024) {
139 NumberFormat megaByteFormat = new DecimalFormat("0.0 MB", new DecimalFormatSymbols());
140 return megaByteFormat.format((double) byteCount / (1024 * 1024));
141 }
142
143
144 if (byteCount >= 1024) {
145 NumberFormat kiloByteFormat = new DecimalFormat("0 KB", new DecimalFormatSymbols());
146 return kiloByteFormat.format((double) byteCount / 1024);
147 }
148
149 return byteCount + " B";
150 }
151
152
153
154
155
156
157
158
159
160 public static String[] split(String input) {
161 if (input == null) {
162 return new String[0];
163 }
164
165 Pattern pattern = Pattern.compile("\".*?\"|\\S+");
166 Matcher matcher = pattern.matcher(input);
167
168 List<String> result = new ArrayList<String>();
169 while (matcher.find()) {
170 String element = matcher.group();
171 if (element.startsWith("\"") && element.endsWith("\"") && element.length() > 1) {
172 element = element.substring(1, element.length() - 1);
173 }
174 result.add(element);
175 }
176
177 return result.toArray(new String[0]);
178 }
179
180
181
182
183
184
185
186
187 public static String[] readLines(InputStream in) throws IOException {
188 BufferedReader reader = null;
189
190 try {
191 reader = new BufferedReader(new InputStreamReader(in));
192 List<String> result = new ArrayList<String>();
193 for (String line = reader.readLine(); line != null; line = reader.readLine()) {
194 line = line.trim();
195 if (!line.startsWith("#") && line.length() > 0) {
196 result.add(line);
197 }
198 }
199 return result.toArray(new String[0]);
200
201 } finally {
202 IOUtils.closeQuietly(in);
203 IOUtils.closeQuietly(reader);
204 }
205 }
206
207
208
209
210
211
212
213
214
215 public static String toHttpUrl(String url, int port) throws MalformedURLException {
216 URL u = new URL(url);
217 if ("https".equals(u.getProtocol())) {
218 return new URL("http", u.getHost(), port, u.getFile()).toString();
219 }
220 return url;
221 }
222
223
224
225
226
227 public static boolean isEqual(Object a, Object b) {
228 return a == null ? b == null : a.equals(b);
229 }
230
231
232
233
234
235
236 public static Locale parseLocale(String s) {
237 if (s == null) {
238 return null;
239 }
240
241 String[] elements = s.split("_");
242
243 if (elements.length == 0) {
244 return new Locale(s, "", "");
245 }
246 if (elements.length == 1) {
247 return new Locale(elements[0], "", "");
248 }
249 if (elements.length == 2) {
250 return new Locale(elements[0], elements[1], "");
251 }
252 return new Locale(elements[0], elements[1], elements[2]);
253 }
254
255
256
257
258
259
260
261 public static String utf8HexEncode(String s) throws Exception {
262 if (s == null) {
263 return null;
264 }
265 byte[] utf8 = s.getBytes(ENCODING_UTF8);
266 return String.valueOf(Hex.encodeHex(utf8));
267 }
268
269
270
271
272
273
274
275 public static String utf8HexDecode(String s) throws Exception {
276 if (s == null) {
277 return null;
278 }
279 return new String(Hex.decodeHex(s.toCharArray()), ENCODING_UTF8);
280 }
281
282
283
284
285
286
287
288
289
290
291 public static List<String> getIndividualWords(String words) {
292 Set<String> internal = new HashSet<String>();
293 List<String> result = new LinkedList<String>();
294 if (words!=null) {
295 String[] kws = words.split("\\s*,\\s*");
296 for(String kw: kws) {
297 String word = capitalize(kw.trim());
298 if (internal.add(word)) {
299 result.add(word);
300 }
301 }
302 }
303 return result;
304 }
305
306
307
308
309
310
311 public static String capitalize(String word) {
312 if(word!=null && word.length()>0) {
313 String capWord = Character.toUpperCase(word.charAt(0)) + word.substring(1);
314 return capWord;
315 } else {
316 return word;
317 }
318 }
319
320
321
322
323
324
325
326 public static boolean contains(List<String> items, String query) {
327 if (query==null) return false;
328 query = query.toLowerCase();
329
330 for (String candidate : items) {
331 if (candidate!=null) {
332 if (query.equals(candidate.toLowerCase())) {
333 return true;
334 }
335 }
336 }
337 return false;
338 }
339
340 }