View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Operations on <code>String</code> that are <code>null</code> safe
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  	 * Turn the string into CDATA - http://en.wikipedia.org/wiki/CDATA
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  	 * If <code>s</code> ends with <code>suffix</code>, remove it
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  	 * If s is null return "" otherwise return s
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 	 * Combine <code>tokens</code> into a <code>String</code>
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 	 * Convert dots to forward slashes and trim.
143 	 */
144 	public static final String getPath(String s) {
145 		return StringUtils.trim(StringUtils.replace(s, DOT, FORWARD_SLASH));
146 	}
147 
148 	/**
149 	 * Surround the string with double quotes.
150 	 */
151 	public static final String quote(String s) {
152 		return s == null ? null : DOUBLE_QUOTE + s + DOUBLE_QUOTE;
153 	}
154 
155 	/**
156 	 * Split comma separated values into tokens, optionally trimming the tokens.
157 	 */
158 	public static final String[] splitCSV(String csv, boolean trim) {
159 		return split(csv, COMMA, trim);
160 	}
161 
162 	/**
163 	 * Split comma separated values into tokens, trimming as we go.
164 	 */
165 	public static final String[] splitAndTrimCSV(String csv) {
166 		return splitCSV(csv, true);
167 	}
168 
169 	/**
170 	 * Split the string into tokens using the indicated separator, trimming as we go.
171 	 */
172 	public static final String[] splitAndTrim(String s, String separatorChars) {
173 		return split(s, separatorChars, true);
174 	}
175 
176 	/**
177 	 * Split the string into tokens using the indicated separator chars, optionally trimming the tokens.
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 	 * Replace carriage returns and linefeeds with a space
192 	 */
193 	public static final String flatten(String s) {
194 		return flatten(s, SPACE, SPACE);
195 	}
196 
197 	/**
198 	 * Replace carriage returns with <code>cr</code> and linefeeds with <code>lf</code>.
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 }