1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mailet.base;
21
22 import java.security.MessageDigest;
23 import java.security.NoSuchAlgorithmException;
24 import java.util.ArrayList;
25
26
27
28
29 public final class StringUtils {
30
31 private StringUtils() {
32
33 }
34
35
36
37
38
39
40
41
42
43 public static String[] split(String text, String pattern) {
44 String[] array = text.split(pattern, -1);
45 ArrayList list = new ArrayList();
46 for (int i = 0; i < array.length; i++) {
47 boolean escaped = false;
48 if (i > 0 && array[i - 1].endsWith("\\")) {
49
50
51 int depth = 1;
52 while (depth < array[i-1].length() && array[i-1].charAt(array[i-1].length() - 1 - depth) == '\\') depth ++;
53 escaped = depth % 2 == 1;
54 }
55 if (!escaped) list.add(array[i]);
56 else {
57 String prev = (String) list.remove(list.size() - 1);
58 list.add(prev.substring(0, prev.length() - 1) + pattern + array[i]);
59 }
60 }
61 return (String[]) list.toArray(new String[0]);
62 }
63
64
65
66
67
68
69
70 public static String md5(java.lang.String message) {
71 try {
72 MessageDigest md = MessageDigest.getInstance("MD5");
73 StringBuffer sb = new StringBuffer();
74 byte buf[] = message.getBytes();
75 byte[] md5 = md.digest(buf);
76
77 for (int i = 0; i < md5.length; i++) {
78 String tmpStr = "0" + Integer.toHexString((0xff & md5[i]));
79 sb.append(tmpStr.substring(tmpStr.length() - 2));
80 }
81 return sb.toString();
82
83 } catch (NoSuchAlgorithmException e) {
84 return null;
85 }
86 }
87
88
89
90
91
92
93
94
95 public static String capitalizeWords(String data) {
96 if (data==null) return null;
97 StringBuffer res = new StringBuffer();
98 char ch;
99 char prevCh = '.';
100 for ( int i = 0; i < data.length(); i++ ) {
101 ch = data.charAt(i);
102 if ( Character.isLetter(ch)) {
103 if (!Character.isLetter(prevCh) ) res.append( Character.toUpperCase(ch) );
104 else res.append( Character.toLowerCase(ch) );
105 } else res.append( ch );
106 prevCh = ch;
107 }
108 return res.toString();
109 }
110 }