1 23 24 package com.sun.enterprise.config.backup.util; 25 26 28 29 import java.util.*; 30 31 public class StringUtils 32 { 33 private StringUtils() 34 { 35 36 } 37 38 40 43 public static int safeLength(String s) 44 { 45 if(s == null) 46 return 0; 47 48 return s.length(); 49 } 50 51 53 public static boolean ok(String s) 54 { 55 return s != null && s.length() > 0; 56 } 57 58 60 public static int maxWidth(Vector v) 61 { 62 int max = 0; 64 65 if(v == null || v.size() <= 0 || !(v.elementAt(0) instanceof String )) 66 return 0; 67 68 for(int i = v.size() - 1; i >= 0; i--) 69 { 70 int len = ((String )v.elementAt(i)).length(); 71 72 if(len > max) 73 max = len; 74 } 75 76 return max; 77 } 78 79 81 public static boolean isHex(String s) 82 { 83 87 final int slen = s.length(); 88 89 for(int i = 0; i < slen; i++) 90 if(isHex(s.charAt(i)) == false) 91 return false; 92 return true; 93 } 94 95 97 public static boolean isHex(char c) 98 { 99 101 String hex = "0123456789abcdefABCDEF"; 102 int hexlen = hex.length(); 103 104 for(int i = 0; i < hexlen; i++) 105 if(hex.charAt(i) == c) 106 return true; 107 108 return false; 109 } 110 111 113 public static String getPenultimateDirName(String s) 114 { 115 117 if(s == null || s.length() <= 0) 118 return s; 119 120 if( (s.indexOf('/') < 0) && (s.indexOf('\\') < 0) ) 122 return ""; 123 124 s = s.replace('\\', '/'); 126 int index = s.lastIndexOf('/'); 127 128 if(index < 0) 129 return ""; 131 s = s.substring(0, index); 133 index = s.lastIndexOf('/'); 134 135 if(index >= 0) 136 s = s.substring(index + 1); 137 138 return s; 139 } 140 141 143 public static String toShortClassName(String className) 144 { 145 int index = className.lastIndexOf('.'); 146 147 if(index >= 0 && index < className.length() - 1) 148 return className.substring(index + 1); 149 150 return className; 151 } 152 153 155 public static String padRight(String s, int len) 156 { 157 if(s == null || s.length() >= len) 158 return s; 159 160 for(int i = len - s.length(); i > 0; --i) 161 s += ' '; 162 163 return s; 164 } 165 166 168 public static String padLeft(String s, int len) 169 { 170 String ss = ""; 171 172 if(s == null || s.length() >= len) 173 return s; 174 175 for(int i = len - s.length(); i > 0; --i) 176 ss += ' '; 177 178 return ss + s; 179 } 180 181 183 public static String [] toLines(String s) 184 { 185 if(s == null) 186 return new String [0]; 187 188 Vector v = new Vector(); 189 190 int start = 0; 191 int end = 0; 192 193 for(end = s.indexOf('\n', start); end >= 0 && start < s.length(); end = s.indexOf('\n', start)) 194 { 195 v.addElement(s.substring(start, end)); start = end + 1; 197 } 198 199 if(start < s.length()) 200 v.addElement(s.substring(start)); 201 202 String [] ss = new String [v.size()]; 203 204 v.copyInto(ss); 205 206 return ss; 207 } 208 209 211 public static void prepend(String [] ss, String what) 212 { 213 for(int i = 0; i < ss.length; i++) 214 ss[i] = what + ss[i]; 215 } 216 217 219 public static String UpperCaseFirstLetter(String s) 220 { 221 if(s == null || s.length() <= 0) 222 return s; 223 224 return s.substring(0, 1).toUpperCase() + s.substring(1); 225 } 226 227 229 public static String replace(String s, String token, String replace) 230 { 231 if(s == null || s.length() <= 0 || token == null || token.length() <= 0) 232 return s; 233 234 int index = s.indexOf(token); 235 236 if(index < 0) 237 return s; 238 239 int tokenLength = token.length(); 240 String ret = s.substring(0, index); 241 ret += replace; 242 ret += s.substring(index + tokenLength); 243 244 return ret; 245 } 246 247 249 public static String toString(Properties props) 250 { 251 if(props == null || props.size() <= 0) 252 return "No entries"; 253 254 Set entries = props.entrySet(); 255 StringBuffer sb = new StringBuffer (); 256 257 int keyWidth = 0; 259 for(Iterator it = entries.iterator(); it.hasNext(); ) 260 { 261 Map.Entry me = (Map.Entry)it.next(); 262 String key = (String )me.getKey(); 263 int len = key.length(); 264 265 if(len > keyWidth) 266 keyWidth = len; 267 } 268 269 ++keyWidth; 270 271 for(Iterator it = entries.iterator(); it.hasNext(); ) 273 { 274 Map.Entry me = (Map.Entry)it.next(); 275 String key = (String )me.getKey(); 276 String val = (String )me.getValue(); 277 278 sb.append(padRight(key, keyWidth)); 279 sb.append("= "); 280 sb.append(val); 281 sb.append('\n'); 282 } 283 284 return sb.toString(); 285 } 286 287 288 290 public static void main(String [] args) 291 { 292 final int len = args.length; 293 294 if((len == 1) && args[0].equalsIgnoreCase("toLine")) 295 testToLine(); 296 else if((len > 1) && args[0].equalsIgnoreCase("isHex")) 297 testHex(args); 298 else 299 usage(); 300 } 301 302 304 private static void usage() 305 { 306 System.out.println("StringUtils -- main() for testing usage:\n"); 307 System.out.println("java netscape.blizzard.util.StringUtils toLine"); 308 System.out.println("java netscape.blizzard.util.StringUtils isHex number1 number2 ..."); 309 } 310 311 313 private static void testHex(String [] args) 314 { 315 System.out.println("StringUtils -- Testing Hex"); 316 317 for(int i = 1; i < args.length; i++) 318 System.out.println(padRight(args[i], 16) + " " + (isHex(args[i]) ? "yesHex" : "notHex")); 319 } 320 321 323 private static void testToLine() 324 { 325 System.out.println("StringUtils -- Testing toLine()"); 326 String [] ss = 327 { 328 null, 329 "", 330 "abc\ndef\n", 331 "abc\ndef", 332 "abc", 333 "abc\n", 334 "abc\n\n", 335 "q", 336 "\n\nk\n\nz\n\n", 337 "sd.adj;ld" 338 }; 339 340 for(int k = 0; k < ss.length; k++) 341 { 342 String [] s2 = StringUtils.toLines(ss[k]); 343 System.out.println("String #" + k + ", Number of Lines: " + s2.length); 344 345 for(int i = 0; i < s2.length; i++) 346 System.out.println(s2[i]); 347 } 348 } 349 350 351 public static void testUpperCase() 352 { 353 String [] test = new String [] { "xyz", "HITHERE", "123aa", "aSSS", "yothere" }; 355 for(int i = 0; i < test.length; i++) 356 { 357 System.out.println(test[i] + " >>> " + UpperCaseFirstLetter(test[i])); } 359 } 360 361 373 374 public static String makeFilePath(String [] strings, boolean addTrailing) 375 { 376 StringBuffer path = null; 377 String separator = System.getProperty("file.separator"); 378 if (strings != null) 379 { 380 path = new StringBuffer (); 381 for (int i = 0 ; i < strings.length ; i++) 382 { 383 String element = strings[i]; 384 if (element == null || element.length () == 0) 385 { 386 throw new IllegalArgumentException (); 387 } 388 path.append(element); 389 if (i < strings.length - 1) 390 { 391 path.append(separator); 392 } 393 } 394 if (addTrailing) 395 { 396 path.append(separator); 397 } 398 } 399 return ( path.toString() ); 400 } 401 402 417 public static List parseStringList(String line) 418 { 419 return parseStringList(line, null); 420 } 421 422 440 public static List parseStringList(String line, String sep) 441 { 442 if (line == null) 443 return null; 444 445 StringTokenizer st; 446 if (sep == null) 447 st = new StringTokenizer(line); 448 else 449 st = new StringTokenizer(line, sep); 450 451 String token; 452 453 List tokens = new Vector(); 454 while (st.hasMoreTokens()) 455 { 456 token = st.nextToken().trim(); 457 if (token.length() > 0) 458 tokens.add(token); 459 } 460 461 return tokens; 462 } 463 464 } 465 | Popular Tags |