1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util;
17
18 import java.io.UnsupportedEncodingException;
19 import java.util.List;
20
21 import org.apache.commons.lang3.StringUtils;
22
23
24
25
26 public class Str {
27
28 public static final String EMPTY_STRING = "";
29 public static final String UTF8 = "UTF-8";
30 public static final String COMMA = ",";
31 public static final String SPACE = " ";
32 public static final String CR = "\r";
33 public static final String LF = "\n";
34 public static final String DOT = ".";
35 public static final String FORWARD_SLASH = "/";
36 public static final char DOUBLE_QUOTE = '"';
37 public static final String CDATA_PREFIX = "<![CDATA[";
38 public static final String CDATA_SUFFIX = "]]>";
39
40
41
42
43 public static final String cdata(String s) {
44 if (s == null) {
45 return null;
46 } else {
47 return CDATA_PREFIX + s + CDATA_SUFFIX;
48 }
49 }
50
51
52
53
54 public static final String removeSuffix(String s, String suffix) {
55 if (StringUtils.endsWith(s, suffix)) {
56 int end = StringUtils.length(s) - StringUtils.length(suffix);
57 return StringUtils.substring(s, 0, end);
58 } else {
59 return s;
60 }
61 }
62
63
64
65
66 public static final String toEmpty(String s) {
67 if (s == null) {
68 return "";
69 } else {
70 return s;
71 }
72 }
73
74 public static final String getString(byte[] bytes, String encoding) {
75 if (bytes == null) {
76 return null;
77 }
78 if (encoding == null) {
79 return new String(bytes);
80 }
81 try {
82 return new String(bytes, encoding);
83 } catch (UnsupportedEncodingException e) {
84 throw new IllegalArgumentException(e);
85 }
86 }
87
88 public static final byte[] getUTF8Bytes(String s) {
89 if (s == null) {
90 return null;
91 } else {
92 return getBytes(s, UTF8);
93 }
94 }
95
96 public static final byte[] getBytes(String s, String encoding) {
97 if (s == null) {
98 return null;
99 }
100 if (encoding == null) {
101 return s.getBytes();
102 }
103 try {
104 return s.getBytes(encoding);
105 } catch (UnsupportedEncodingException e) {
106 throw new IllegalArgumentException(e);
107 }
108 }
109
110 public static final boolean contains(List<String> tokens, String value, boolean caseSensitive) {
111 for (String token : tokens) {
112 if (equals(token, value, caseSensitive)) {
113 return true;
114 }
115 }
116 return false;
117 }
118
119 public static final boolean equals(String s1, String s2, boolean caseSensitive) {
120 if (caseSensitive) {
121 return StringUtils.equals(s1, s2);
122 } else {
123 return StringUtils.equalsIgnoreCase(s1, s2);
124 }
125 }
126
127
128
129
130 public static final String toString(String[] tokens) {
131 if (tokens == null) {
132 return null;
133 }
134 StringBuilder sb = new StringBuilder();
135 for (String token : tokens) {
136 sb.append(token);
137 }
138 return sb.toString();
139 }
140
141
142
143
144 public static final String getPath(String s) {
145 return StringUtils.trim(StringUtils.replace(s, DOT, FORWARD_SLASH));
146 }
147
148
149
150
151 public static final String quote(String s) {
152 return s == null ? null : DOUBLE_QUOTE + s + DOUBLE_QUOTE;
153 }
154
155
156
157
158 public static final String[] splitCSV(String csv, boolean trim) {
159 return split(csv, COMMA, trim);
160 }
161
162
163
164
165 public static final String[] splitAndTrimCSV(String csv) {
166 return splitCSV(csv, true);
167 }
168
169
170
171
172 public static final String[] splitAndTrim(String s, String separatorChars) {
173 return split(s, separatorChars, true);
174 }
175
176
177
178
179 public static final String[] split(String s, String separatorChars, boolean trim) {
180 String[] tokens = StringUtils.split(s, separatorChars);
181 if (tokens == null) {
182 return null;
183 }
184 for (int i = 0; i < tokens.length; i++) {
185 tokens[i] = trim ? StringUtils.trim(tokens[i]) : tokens[i];
186 }
187 return tokens;
188 }
189
190
191
192
193 public static final String flatten(String s) {
194 return flatten(s, SPACE, SPACE);
195 }
196
197
198
199
200 public static final String flatten(String s, String cr, String lf) {
201 return StringUtils.replace(StringUtils.replace(s, CR, cr), LF, lf);
202 }
203 }