1 23 24 package com.sun.enterprise.util; 25 26 28 29 import java.util.*; 30 import java.sql.SQLException ; 31 32 public class StringUtils 33 { 34 private StringUtils() 35 { 36 37 } 38 39 41 44 public static int safeLength(String s) 45 { 46 if(s == null) 47 return 0; 48 49 return s.length(); 50 } 51 52 54 public static boolean ok(String s) 55 { 56 return s != null && s.length() > 0; 57 } 58 59 61 public static String formatSQLException(SQLException ex) 62 { 63 Assertion.check(ex); 64 65 String s = "SQLException:\n"; 66 67 do 68 { 69 s += "SQLState: " + ex.getSQLState() + "\n"; 70 s += "Message: " + ex.getMessage() + "\n"; 71 s += "Vendor: " + ex.getErrorCode()+ "\n"; 72 s += "\n"; 73 }while( (ex = ex.getNextException()) != null); 74 75 return s; 76 } 77 78 80 public static int maxWidth(Vector v) 81 { 82 int max = 0; 84 85 if(v == null || v.size() <= 0 || !(v.elementAt(0) instanceof String )) 86 return 0; 87 88 for(int i = v.size() - 1; i >= 0; i--) 89 { 90 int len = ((String )v.elementAt(i)).length(); 91 92 if(len > max) 93 max = len; 94 } 95 96 return max; 97 } 98 99 101 public static boolean isHex(String s) 102 { 103 107 final int slen = s.length(); 108 109 for(int i = 0; i < slen; i++) 110 if(isHex(s.charAt(i)) == false) 111 return false; 112 return true; 113 } 114 115 117 public static boolean isHex(char c) 118 { 119 121 String hex = "0123456789abcdefABCDEF"; 122 int hexlen = hex.length(); 123 124 for(int i = 0; i < hexlen; i++) 125 if(hex.charAt(i) == c) 126 return true; 127 128 return false; 129 } 130 131 133 public static String getPenultimateDirName(String s) 134 { 135 137 if(s == null || s.length() <= 0) 138 return s; 139 140 if( (s.indexOf('/') < 0) && (s.indexOf('\\') < 0) ) 142 return ""; 143 144 s = s.replace('\\', '/'); 146 int index = s.lastIndexOf('/'); 147 148 if(index < 0) 149 return ""; 151 s = s.substring(0, index); 153 index = s.lastIndexOf('/'); 154 155 if(index >= 0) 156 s = s.substring(index + 1); 157 158 return s; 159 } 160 161 163 public static String toShortClassName(String className) 164 { 165 int index = className.lastIndexOf('.'); 166 167 if(index >= 0 && index < className.length() - 1) 168 return className.substring(index + 1); 169 170 return className; 171 } 172 173 175 public static String padRight(String s, int len) 176 { 177 if(s == null || s.length() >= len) 178 return s; 179 180 for(int i = len - s.length(); i > 0; --i) 181 s += ' '; 182 183 return s; 184 } 185 186 188 public static String padLeft(String s, int len) 189 { 190 String ss = ""; 191 192 if(s == null || s.length() >= len) 193 return s; 194 195 for(int i = len - s.length(); i > 0; --i) 196 ss += ' '; 197 198 return ss + s; 199 } 200 201 203 public static String [] toLines(String s) 204 { 205 if(s == null) 206 return new String [0]; 207 208 Vector v = new Vector(); 209 210 int start = 0; 211 int end = 0; 212 213 for(end = s.indexOf('\n', start); end >= 0 && start < s.length(); end = s.indexOf('\n', start)) 214 { 215 v.addElement(s.substring(start, end)); start = end + 1; 217 } 218 219 if(start < s.length()) 220 v.addElement(s.substring(start)); 221 222 String [] ss = new String [v.size()]; 223 224 v.copyInto(ss); 225 226 return ss; 227 } 228 229 231 public static void prepend(String [] ss, String what) 232 { 233 for(int i = 0; i < ss.length; i++) 234 ss[i] = what + ss[i]; 235 } 236 237 239 public static String UpperCaseFirstLetter(String s) 240 { 241 if(s == null || s.length() <= 0) 242 return s; 243 244 return s.substring(0, 1).toUpperCase() + s.substring(1); 245 } 246 247 249 public static String replace(String s, String token, String replace) 250 { 251 if(s == null || s.length() <= 0 || token == null || token.length() <= 0) 252 return s; 253 254 int index = s.indexOf(token); 255 256 if(index < 0) 257 return s; 258 259 int tokenLength = token.length(); 260 String ret = s.substring(0, index); 261 ret += replace; 262 ret += s.substring(index + tokenLength); 263 264 return ret; 265 } 266 267 269 public static String toString(Properties props) 270 { 271 if(props == null || props.size() <= 0) 272 return "No entries"; 273 274 Set entries = props.entrySet(); 275 StringBuffer sb = new StringBuffer (); 276 277 int keyWidth = 0; 279 for(Iterator it = entries.iterator(); it.hasNext(); ) 280 { 281 Map.Entry me = (Map.Entry)it.next(); 282 String key = (String )me.getKey(); 283 int len = key.length(); 284 285 if(len > keyWidth) 286 keyWidth = len; 287 } 288 289 ++keyWidth; 290 291 for(Iterator it = entries.iterator(); it.hasNext(); ) 293 { 294 Map.Entry me = (Map.Entry)it.next(); 295 String key = (String )me.getKey(); 296 String val = (String )me.getValue(); 297 298 sb.append(padRight(key, keyWidth)); 299 sb.append("= "); 300 sb.append(val); 301 sb.append('\n'); 302 } 303 304 return sb.toString(); 305 } 306 307 308 310 public static void main(String [] args) 311 { 312 final int len = args.length; 313 314 if((len == 1) && args[0].equalsIgnoreCase("toLine")) 315 testToLine(); 316 else if((len > 1) && args[0].equalsIgnoreCase("isHex")) 317 testHex(args); 318 else 319 usage(); 320 } 321 322 324 private static void usage() 325 { 326 System.out.println("StringUtils -- main() for testing usage:\n"); 327 System.out.println("java netscape.blizzard.util.StringUtils toLine"); 328 System.out.println("java netscape.blizzard.util.StringUtils isHex number1 number2 ..."); 329 } 330 331 333 private static void testHex(String [] args) 334 { 335 System.out.println("StringUtils -- Testing Hex"); 336 337 for(int i = 1; i < args.length; i++) 338 System.out.println(padRight(args[i], 16) + " " + (isHex(args[i]) ? "yesHex" : "notHex")); 339 } 340 341 343 private static void testToLine() 344 { 345 System.out.println("StringUtils -- Testing toLine()"); 346 String [] ss = 347 { 348 null, 349 "", 350 "abc\ndef\n", 351 "abc\ndef", 352 "abc", 353 "abc\n", 354 "abc\n\n", 355 "q", 356 "\n\nk\n\nz\n\n", 357 "sd.adj;ld" 358 }; 359 360 for(int k = 0; k < ss.length; k++) 361 { 362 String [] s2 = StringUtils.toLines(ss[k]); 363 System.out.println("String #" + k + ", Number of Lines: " + s2.length); 364 365 for(int i = 0; i < s2.length; i++) 366 System.out.println(s2[i]); 367 } 368 } 369 370 371 public static void testUpperCase() 372 { 373 String [] test = new String [] { "xyz", "HITHERE", "123aa", "aSSS", "yothere" }; 375 for(int i = 0; i < test.length; i++) 376 { 377 System.out.println(test[i] + " >>> " + UpperCaseFirstLetter(test[i])); } 379 } 380 381 393 394 public static String makeFilePath(String [] strings, boolean addTrailing) 395 { 396 StringBuffer path = null; 397 String separator = System.getProperty("file.separator"); 398 if (strings != null) 399 { 400 path = new StringBuffer (); 401 for (int i = 0 ; i < strings.length ; i++) 402 { 403 String element = strings[i]; 404 if (element == null || element.length () == 0) 405 { 406 throw new IllegalArgumentException (); 407 } 408 path.append(element); 409 if (i < strings.length - 1) 410 { 411 path.append(separator); 412 } 413 } 414 if (addTrailing) 415 { 416 path.append(separator); 417 } 418 } 419 return ( path.toString() ); 420 } 421 422 437 public static List parseStringList(String line) 438 { 439 return parseStringList(line, null); 440 } 441 442 460 public static List parseStringList(String line, String sep) 461 { 462 if (line == null) 463 return null; 464 465 StringTokenizer st; 466 if (sep == null) 467 st = new StringTokenizer(line); 468 else 469 st = new StringTokenizer(line, sep); 470 471 String token; 472 473 List tokens = new Vector(); 474 while (st.hasMoreTokens()) 475 { 476 token = st.nextToken().trim(); 477 if (token.length() > 0) 478 tokens.add(token); 479 } 480 481 return tokens; 482 } 483 484 } 485 | Popular Tags |