View Javadoc

1   /**
2    * Copyright 2010-2012 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  
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  	 * Combine <code>tokens</code> into a <code>String</code>
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 	 * Convert dots to forward slashes and trim.
107 	 */
108 	public static final String getPath(String s) {
109 		return StringUtils.trim(StringUtils.replace(s, DOT, FORWARD_SLASH));
110 	}
111 
112 	/**
113 	 * Surround the string with double quotes.
114 	 */
115 	public static final String quote(String s) {
116 		return s == null ? null : DOUBLE_QUOTE + s + DOUBLE_QUOTE;
117 	}
118 
119 	/**
120 	 * Split comma separated values into tokens, optionally trimming the tokens.
121 	 */
122 	public static final String[] splitCSV(String csv, boolean trim) {
123 		return split(csv, COMMA, trim);
124 	}
125 
126 	/**
127 	 * Split comma separated values into tokens, trimming as we go.
128 	 */
129 	public static final String[] splitAndTrimCSV(String csv) {
130 		return splitCSV(csv, true);
131 	}
132 
133 	/**
134 	 * Split the string into tokens using the indicated separator, trimming as we go.
135 	 */
136 	public static final String[] splitAndTrim(String s, String separatorChars) {
137 		return split(s, separatorChars, true);
138 	}
139 
140 	/**
141 	 * Split the string into tokens using the indicated separator chars, optionally trimming the tokens.
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 	 * Replace carriage returns and linefeeds with spaces.
156 	 */
157 	public static final String flatten(String s) {
158 		return flatten(s, SPACE, SPACE);
159 	}
160 
161 	/**
162 	 * Replace carriage returns with <code>cr</code> and linefeeds with <code>lf</code>.
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 }