1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.isenshi.util;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.ObjectInputStream;
23 import java.io.ObjectOutputStream;
24 import java.io.UnsupportedEncodingException;
25 import java.lang.reflect.InvocationTargetException;
26 import java.net.URLDecoder;
27 import java.net.URLEncoder;
28 import java.sql.Timestamp;
29 import java.text.SimpleDateFormat;
30 import java.util.Calendar;
31 import java.util.Collection;
32 import java.util.Date;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.Map;
36
37 import org.apache.commons.beanutils.PropertyUtils;
38 import org.apache.commons.lang.ArrayUtils;
39 import org.seasar.tuigwaa.cms.ContentsStoreServiceSlideImpl;
40
41 /***
42 * @author nishioka
43 */
44 public class CharUtil {
45
46 private static final String URL_ENCODING = "UTF-8";
47
48 private static final String NULL_STRING = "";
49
50 public static String[] removeArrays(String[] array1, String[] array2) {
51 if (array2 == null) {
52 return array1;
53 }
54 int length = array1.length - array2.length;
55 String[] ret = new String[length];
56 int j = 0;
57 for (int i = 0; i < array1.length; i++) {
58 if (!ArrayUtils.contains(array2, array1[i])) {
59 ret[j] = array1[i];
60 j++;
61 }
62 }
63 return ret;
64 }
65
66 public static String urlDecode(String s, String enc) {
67 if (!"Windows-31J".equals(enc)) {
68 return urlDecode(s);
69 }
70
71 boolean needToChange = false;
72 StringBuffer sb = new StringBuffer();
73 int numChars = s.length();
74 int i = 0;
75 while (i < numChars) {
76 char c = s.charAt(i);
77 switch (c) {
78 case '+':
79 sb.append(' ');
80 i++;
81 needToChange = true;
82 break;
83 case '%':
84 try {
85
86 byte[] bytes = new byte[((numChars - i) / 3) * 2];
87 int pos = 0;
88 int charCount = 0;
89
90 while (((i + 2) < numChars) && (c == '%')) {
91 bytes[pos++] = (byte) Integer.parseInt(s.substring(
92 i + 1, i + 3), 16);
93 i += 3;
94 charCount++;
95
96 if (i < numChars) {
97 c = s.charAt(i);
98
99
100 int tmpPos = pos - 1;
101 if(bytes[tmpPos] > 0 && charCount%2==1){
102 charCount ++;
103 }
104 if (charCount % 2 == 1){
105 if (c != '%' && (i + 1) <= numChars) {
106 String str = s.substring(i, i + 1);
107 bytes[pos++] = str.getBytes()[0];
108 i++;
109 charCount++;
110 if (i == numChars) {
111 break;
112 }
113 c = s.charAt(i);
114 }
115 }
116 }
117 }
118
119
120
121
122 if ((i < numChars) && (c == '%'))
123 throw new IllegalArgumentException(
124 "URLDecoder: Incomplete trailing escape (%) pattern");
125
126 String str = new String(bytes, 0, pos, enc);
127
128 sb.append(str);
129 } catch (NumberFormatException e) {
130 throw new IllegalArgumentException(
131 "URLDecoder: Illegal hex characters in escape (%) pattern - "
132 + e.getMessage());
133 } catch (UnsupportedEncodingException e) {
134 e.printStackTrace();
135 }
136 needToChange = true;
137 break;
138 default:
139 sb.append(c);
140 i++;
141 break;
142 }
143 }
144 return (needToChange ? sb.toString() : s);
145 }
146
147 public static String urlDecode(String value) {
148
149 if (value == null)
150 return null;
151
152 try {
153 value = URLDecoder.decode(value, URL_ENCODING);
154 } catch (UnsupportedEncodingException e) {
155 e.printStackTrace();
156 throw new RuntimeException(e);
157 }
158 return value;
159 }
160
161 public static String urlEncode(String value) {
162 return urlEncode(value, URL_ENCODING);
163 }
164
165 public static String urlEncode(String value, String encoding) {
166 if (value == null)
167 return null;
168
169 try {
170 String[] paths = value.split("/");
171 String ret = NULL_STRING;
172 for (int i = 0; i < paths.length; i++) {
173 if (i != 0) {
174 ret += "/";
175 }
176 ret += URLEncoder.encode(paths[i], encoding);
177 }
178 return ret;
179 } catch (UnsupportedEncodingException e) {
180 e.printStackTrace();
181 throw new RuntimeException(e);
182 }
183 }
184
185 public static String replace(String template, String value) {
186 return replace(template, "0", value);
187 }
188
189 public static String replace(String template, String key, String value) {
190 Map map = new HashMap();
191 map.put(key, value);
192 return replace(template, map);
193 }
194
195 public static String replace(String template, Map context) {
196 if (context == null || template == null) {
197 return null;
198 }
199 for (Iterator i = context.keySet().iterator(); i.hasNext();) {
200 String key = (String) i.next();
201 Object value = context.get(key);
202 if (value == null) {
203 continue;
204 }
205 String regex = "//$//{" + key + "//}";
206 template = template.replaceAll(regex, value.toString());
207 }
208 return template;
209 }
210
211 public static String toUpperCaseAtFisrtChar(String str) {
212 if (str == null) {
213 return null;
214 }
215 String aa = str.substring(0, 1);
216 String bb = str.substring(1);
217 return aa.toUpperCase() + bb;
218 }
219
220 public static byte[] toBytes(Object obj) {
221 ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
222 try {
223 ObjectOutputStream ostream = new ObjectOutputStream(byteStream);
224 ostream.writeObject(obj);
225 ostream.close();
226 } catch (IOException e) {
227 e.printStackTrace();
228 throw new RuntimeException(e);
229 }
230 return byteStream.toByteArray();
231 }
232
233 public static Object toObject(byte[] bytes) {
234 if (bytes == null) {
235 return null;
236 }
237
238 Object obj = null;
239
240 try {
241 ObjectInputStream iStream = new ObjectInputStream(
242 new ByteArrayInputStream(bytes));
243 obj = iStream.readObject();
244
245 } catch (ClassNotFoundException e) {
246 throw new RuntimeException(e);
247 } catch (IOException e) {
248 throw new RuntimeException(e);
249 }
250
251 return obj;
252 }
253
254 public static int childSize(Object bean, String prop) {
255
256 Object obj = getProperty(bean, prop);
257 if (obj instanceof Map) {
258 return ((Map) obj).size();
259 }
260
261 if (obj instanceof Collection) {
262 return ((Collection) obj).size();
263 }
264 return ((Collection) obj).size();
265 }
266
267 public static int size(Object[] array) {
268 if (array == null) {
269 return 0;
270 }
271 return array.length;
272 }
273
274 public static int size(Collection col) {
275 if (col == null)
276 return 0;
277 return col.size();
278 }
279
280 public static int sizeMap(Map col) {
281 if (col == null)
282 return 0;
283 return col.size();
284 }
285
286 public static Object getProperty(Object bean, String prop) {
287 Object ret = null;
288 try {
289 ret = PropertyUtils.getProperty(bean, prop);
290 } catch (IllegalAccessException e) {
291 e.printStackTrace();
292 throw new RuntimeException(e);
293 } catch (InvocationTargetException e) {
294 e.printStackTrace();
295 throw new RuntimeException(e);
296 } catch (NoSuchMethodException e) {
297 e.printStackTrace();
298 throw new RuntimeException(e);
299 }
300 return ret;
301 }
302
303 public static String[] stringToArray(String arrayStr) {
304 return arrayStr.split(",");
305 }
306
307 public static String arrayToString(Object[] array) {
308 StringBuffer buf = new StringBuffer();
309
310 for (int i = 0; i < array.length; i++) {
311 if (i > 0) {
312 buf.append(",");
313 }
314 buf.append(array[i].toString());
315 }
316
317 return buf.toString();
318 }
319
320 /***
321 * Trims the last character from String if it matches with specifid values.<br/>
322 * examples : chartrim("/some/directory/",'/') returns "/some/directory",
323 * chartrim("/some/directory",'/') returns original string.
324 */
325 public static String chartrim(String s, char c) {
326 if (s == null || s.length() == 0) {
327 return s;
328 }
329 int lastidx = s.length() - 1;
330 return (s.charAt(lastidx) == c) ? s.substring(0, lastidx) : s;
331 }
332
333 /***
334 * Trims the first character from String if it matches with specifid values.<br/>
335 * examples : charpop("/some/directory/",'/') returns "some/directory",
336 * chartrim("some/directory",'/') returns original string.
337 */
338 public static String charpop(String s, char c) {
339 if (s == null) {
340 return null;
341 } else if (s.length() < 2) {
342 return s;
343 }
344 return (s.charAt(0) == c) ? s.substring(1) : s;
345 }
346
347 public static int count(String s, char c) {
348 String cs = String.valueOf(c);
349 String[] array = s.split(cs);
350 return array.length - 1;
351 }
352
353 /***
354 * If str doesn't contain prefix or suffix , the return value is null.
355 *
356 * @param str
357 * @param prefix
358 * @param suffix
359 */
360 public static String removePrefixAndSuffix(String str, String prefix,
361 String suffix) {
362 if (str == null || prefix == null || suffix == null) {
363 return null;
364 }
365
366 if (str.startsWith(prefix) && str.endsWith(suffix)) {
367 return str.substring(prefix.length(), str.length()
368 - suffix.length());
369 }
370 return null;
371 }
372
373 public static InputStream toInputStream(String data) {
374 if (data == null) {
375 return null;
376 }
377 ByteArrayInputStream bis = null;
378 try {
379 byte[] bytes;
380 try {
381 bytes = data
382 .getBytes(ContentsStoreServiceSlideImpl.DEFAULT_ENCODING);
383 bis = new ByteArrayInputStream(bytes);
384 } catch (UnsupportedEncodingException e) {
385
386 }
387 } finally {
388 if (bis != null) {
389 try {
390 bis.close();
391 } catch (IOException e) {
392
393 }
394 }
395 }
396 return bis;
397 }
398
399 public static final String join(String[] array, String joinstr) {
400 return join(0, array, joinstr);
401 }
402
403 public static final String join(int startIndex, String[] array,
404 String joinstr) {
405 if (array == null)
406 return null;
407
408 int size = array.length;
409
410 StringBuffer buf = new StringBuffer();
411 for (int i = startIndex; i < size; i++) {
412 buf.append(array[i]);
413 if (size != startIndex && i != size - 1) {
414 buf.append(joinstr);
415 }
416 }
417 return buf.toString();
418 }
419
420 public static final String createQuery(Map params) {
421 StringBuffer buf = new StringBuffer();
422 boolean isFisrt = true;
423 for (Iterator i = params.keySet().iterator(); i.hasNext();) {
424 String key = (String) i.next();
425 String value = (String) params.get(key);
426 if (value == null) {
427 continue;
428 }
429 if (isFisrt) {
430 isFisrt = false;
431 } else {
432 buf.append("&");
433 }
434 buf.append(CharUtil.urlEncode(key) + "="
435 + CharUtil.urlEncode(value));
436 }
437 return buf.toString();
438 }
439
440 public static final String removeReturnSequence(String letter) {
441 if (letter == null)
442 return NULL_STRING;
443
444 letter = letter.replaceAll("\r", NULL_STRING);
445 letter = letter.replaceAll("\n", NULL_STRING);
446 return letter;
447 }
448
449 public static final boolean stringEquals(Object a, Object b) {
450 if (a == null || b == null) {
451 return false;
452 }
453 return String.valueOf(a).equals(String.valueOf(b));
454 }
455
456 public static final int compare(Comparable a, Comparable b) {
457 if (a == null || b == null) {
458 return -1;
459 }
460 return a.compareTo(b);
461 }
462
463 public static final boolean isNull(String str) {
464 return NULL_STRING.equals(str);
465 }
466
467 public static final Date today() {
468 return CalendarUtils.getToday().getTime();
469 }
470
471 public static final Date tommorrow() {
472 Calendar cal = CalendarUtils.getToday();
473 cal.add(Calendar.DATE, 1);
474 return cal.getTime();
475 }
476
477 public static final Date yesterday() {
478 Calendar cal = CalendarUtils.getToday();
479 cal.add(Calendar.DATE, -1);
480 return cal.getTime();
481 }
482
483 public static final String dateformat(Date date, String format) {
484 if(date == null){
485 return "";
486 }
487 SimpleDateFormat dateformat = new SimpleDateFormat(format);
488 return dateformat.format(date);
489 }
490
491 public static final String timestampformat(Timestamp timestamp, String format){
492 return dateformat(new Date(timestamp.getTime()), format);
493 }
494 }