001 /**
002 * Copyright 2010-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.common.util;
017
018 import java.io.UnsupportedEncodingException;
019 import java.util.List;
020
021 import org.apache.commons.lang3.StringUtils;
022
023 /**
024 * Operations on <code>String</code> that are <code>null</code> safe
025 */
026 public class Str {
027
028 public static final String EMPTY_STRING = "";
029 public static final String UTF8 = "UTF-8";
030 public static final String COMMA = ",";
031 public static final String SPACE = " ";
032 public static final String CR = "\r";
033 public static final String LF = "\n";
034 public static final String DOT = ".";
035 public static final String FORWARD_SLASH = "/";
036 public static final char DOUBLE_QUOTE = '"';
037 public static final String CDATA_PREFIX = "<![CDATA[";
038 public static final String CDATA_SUFFIX = "]]>";
039
040 /**
041 * Turn the string into CDATA - http://en.wikipedia.org/wiki/CDATA
042 */
043 public static final String cdata(String s) {
044 if (s == null) {
045 return null;
046 } else {
047 return CDATA_PREFIX + s + CDATA_SUFFIX;
048 }
049 }
050
051 /**
052 * If <code>s</code> ends with <code>suffix</code>, remove it
053 */
054 public static final String removeSuffix(String s, String suffix) {
055 if (StringUtils.endsWith(s, suffix)) {
056 int end = StringUtils.length(s) - StringUtils.length(suffix);
057 return StringUtils.substring(s, 0, end);
058 } else {
059 return s;
060 }
061 }
062
063 /**
064 * If s is null return "" otherwise return s
065 */
066 public static final String toEmpty(String s) {
067 if (s == null) {
068 return "";
069 } else {
070 return s;
071 }
072 }
073
074 public static final String getString(byte[] bytes, String encoding) {
075 if (bytes == null) {
076 return null;
077 }
078 if (encoding == null) {
079 return new String(bytes);
080 }
081 try {
082 return new String(bytes, encoding);
083 } catch (UnsupportedEncodingException e) {
084 throw new IllegalArgumentException(e);
085 }
086 }
087
088 public static final byte[] getUTF8Bytes(String s) {
089 if (s == null) {
090 return null;
091 } else {
092 return getBytes(s, UTF8);
093 }
094 }
095
096 public static final byte[] getBytes(String s, String encoding) {
097 if (s == null) {
098 return null;
099 }
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 }