001 /**
002 * Copyright 2010-2012 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
038 public static final String getString(byte[] bytes, String encoding) {
039 if (bytes == null) {
040 return null;
041 }
042 if (encoding == null) {
043 return new String(bytes);
044 }
045 try {
046 return new String(bytes, encoding);
047 } catch (UnsupportedEncodingException e) {
048 throw new IllegalArgumentException(e);
049 }
050 }
051
052 public static final byte[] getUTF8Bytes(String s) {
053 if (s == null) {
054 return null;
055 } else {
056 return getBytes(s, UTF8);
057 }
058 }
059
060 public static final byte[] getBytes(String s, String encoding) {
061 if (s == null) {
062 return null;
063 }
064 if (encoding == null) {
065 return s.getBytes();
066 }
067 try {
068 return s.getBytes(encoding);
069 } catch (UnsupportedEncodingException e) {
070 throw new IllegalArgumentException(e);
071 }
072 }
073
074 public static final boolean contains(List<String> tokens, String value, boolean caseSensitive) {
075 for (String token : tokens) {
076 if (equals(token, value, caseSensitive)) {
077 return true;
078 }
079 }
080 return false;
081 }
082
083 public static final boolean equals(String s1, String s2, boolean caseSensitive) {
084 if (caseSensitive) {
085 return StringUtils.equals(s1, s2);
086 } else {
087 return StringUtils.equalsIgnoreCase(s1, s2);
088 }
089 }
090
091 /**
092 * Combine <code>tokens</code> into a <code>String</code>
093 */
094 public static final String toString(String[] tokens) {
095 if (tokens == null) {
096 return null;
097 }
098 StringBuilder sb = new StringBuilder();
099 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 a space
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 }