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
38 public static final String getString(byte[] bytes, String encoding) {
39 if (bytes == null) {
40 return null;
41 }
42 if (encoding == null) {
43 return new String(bytes);
44 }
45 try {
46 return new String(bytes, encoding);
47 } catch (UnsupportedEncodingException e) {
48 throw new IllegalArgumentException(e);
49 }
50 }
51
52 public static final byte[] getUTF8Bytes(String s) {
53 if (s == null) {
54 return null;
55 } else {
56 return getBytes(s, UTF8);
57 }
58 }
59
60 public static final byte[] getBytes(String s, String encoding) {
61 if (s == null) {
62 return null;
63 }
64 if (encoding == null) {
65 return s.getBytes();
66 }
67 try {
68 return s.getBytes(encoding);
69 } catch (UnsupportedEncodingException e) {
70 throw new IllegalArgumentException(e);
71 }
72 }
73
74 public static final boolean contains(List<String> tokens, String value, boolean caseSensitive) {
75 for (String token : tokens) {
76 if (equals(token, value, caseSensitive)) {
77 return true;
78 }
79 }
80 return false;
81 }
82
83 public static final boolean equals(String s1, String s2, boolean caseSensitive) {
84 if (caseSensitive) {
85 return StringUtils.equals(s1, s2);
86 } else {
87 return StringUtils.equalsIgnoreCase(s1, s2);
88 }
89 }
90
91
92
93
94 public static final String toString(String[] tokens) {
95 if (tokens == null) {
96 return null;
97 }
98 StringBuilder sb = new StringBuilder();
99 for (String token : tokens) {
100 sb.append(token);
101 }
102 return sb.toString();
103 }
104
105
106
107
108 public static final String getPath(String s) {
109 return StringUtils.trim(StringUtils.replace(s, DOT, FORWARD_SLASH));
110 }
111
112
113
114
115 public static final String quote(String s) {
116 return s == null ? null : DOUBLE_QUOTE + s + DOUBLE_QUOTE;
117 }
118
119
120
121
122 public static final String[] splitCSV(String csv, boolean trim) {
123 return split(csv, COMMA, trim);
124 }
125
126
127
128
129 public static final String[] splitAndTrimCSV(String csv) {
130 return splitCSV(csv, true);
131 }
132
133
134
135
136 public static final String[] splitAndTrim(String s, String separatorChars) {
137 return split(s, separatorChars, true);
138 }
139
140
141
142
143 public static final String[] split(String s, String separatorChars, boolean trim) {
144 String[] tokens = StringUtils.split(s, separatorChars);
145 if (tokens == null) {
146 return null;
147 }
148 for (int i = 0; i < tokens.length; i++) {
149 tokens[i] = trim ? StringUtils.trim(tokens[i]) : tokens[i];
150 }
151 return tokens;
152 }
153
154
155
156
157 public static final String flatten(String s) {
158 return flatten(s, SPACE, SPACE);
159 }
160
161
162
163
164 public static final String flatten(String s, String cr, String lf) {
165 return StringUtils.replace(StringUtils.replace(s, CR, cr), LF, lf);
166 }
167 }