1 package org.apache.turbine.util; 2 3 18 19 import java.util.NoSuchElementException ; 20 import java.util.StringTokenizer ; 21 22 import org.apache.commons.lang.exception.ExceptionUtils; 23 24 36 public class StringUtils 37 { 38 46 public static final String makeString(String foo) 47 { 48 return org.apache.commons.lang.StringUtils.defaultString(foo); 49 } 50 51 59 public static final boolean isValid(String foo) 60 { 61 return org.apache.commons.lang.StringUtils.isNotEmpty(foo); 62 } 63 64 71 public static final boolean isEmpty(String foo) 72 { 73 return org.apache.commons.lang.StringUtils.isEmpty(foo); 74 } 75 76 83 public static final String stackTrace(Throwable e) 84 { 85 return ExceptionUtils.getStackTrace(e); 86 } 87 88 95 public static final String stackTrace(Throwable e, boolean addPre) 96 { 97 if (addPre) 98 { 99 return "<pre>" + ExceptionUtils.getStackTrace(e) + "</pre>"; 100 } 101 else 102 { 103 return ExceptionUtils.getStackTrace(e); 104 } 105 } 106 107 116 public static boolean equals(String s1, String s2) 117 { 118 return org.apache.commons.lang.StringUtils.equals(s1, s2); 119 } 120 121 public static final int PPKEY_CLASSNAME = 0; 122 public static final int PPKEY_ID = 1; 123 public static final int PPKEY_PROPERTY = 2; 124 125 131 public static String [] parseObjectKey(String s) 132 { 133 String [] p = new String [3]; 134 StringTokenizer st = new StringTokenizer (s, "[]"); 135 int count = st.countTokens(); 136 if (count > 1) 137 { 138 p[0] = st.nextToken(); 139 p[1] = st.nextToken(); 140 if (count == 3) 141 { 142 p[2] = st.nextToken(); 143 } 144 } 145 return p; 146 } 147 148 152 public static String removeUnderScores(String data) 153 { 154 155 String temp = null; 156 StringBuffer out = new StringBuffer (); 157 temp = data; 158 159 StringTokenizer st = new StringTokenizer (temp, "_"); 160 while (st.hasMoreTokens()) 161 { 162 String element = (String ) st.nextElement(); 163 out.append(org.apache.commons.lang.StringUtils.capitalise(element)); 164 } 165 return out.toString(); 166 } 167 168 173 public static String firstLetterCaps(String data) 174 { 175 return org.apache.commons.lang.StringUtils.capitalise(data); 176 } 177 178 186 public static String [] split(String text, String separator) 187 { 188 return org.apache.commons.lang.StringUtils.split(text, separator); 189 } 190 191 200 public static String join(String [] list, String separator) 201 { 202 return org.apache.commons.lang.StringUtils.join(list, separator); 203 } 204 205 218 219 public static String wrapText(String inString, String newline, 220 int wrapColumn) 221 { 222 StringTokenizer lineTokenizer = new StringTokenizer ( 223 inString, newline, true); 224 StringBuffer stringBuffer = new StringBuffer (); 225 226 while (lineTokenizer.hasMoreTokens()) 227 { 228 try 229 { 230 String nextLine = lineTokenizer.nextToken(); 231 232 if (nextLine.length() > wrapColumn) 233 { 234 nextLine = wrapLine(nextLine, newline, wrapColumn); 236 } 237 238 stringBuffer.append(nextLine); 239 } 240 catch (NoSuchElementException nsee) 241 { 242 break; 244 } 245 } 246 247 return (stringBuffer.toString()); 248 } 249 250 260 261 protected static String wrapLine(String line, String newline, 262 int wrapColumn) 263 { 264 StringBuffer wrappedLine = new StringBuffer (); 265 266 while (line.length() > wrapColumn) 267 { 268 int spaceToWrapAt = line.lastIndexOf(' ', wrapColumn); 269 270 if (spaceToWrapAt >= 0) 271 { 272 wrappedLine.append(line.substring(0, spaceToWrapAt)); 273 wrappedLine.append(newline); 274 line = line.substring(spaceToWrapAt + 1); 275 } 276 277 else 283 { 284 spaceToWrapAt = line.indexOf(' ', wrapColumn); 285 286 if (spaceToWrapAt >= 0) 287 { 288 wrappedLine.append(line.substring(0, spaceToWrapAt)); 289 wrappedLine.append(newline); 290 line = line.substring(spaceToWrapAt + 1); 291 } 292 else 293 { 294 wrappedLine.append(line); 295 line = ""; 296 } 297 } 298 } 299 300 wrappedLine.append(line); 303 304 return wrappedLine.toString(); 305 } 306 } 307 | Popular Tags |