1 package com.teamkonzept.lib; 2 3 import java.io.*; 4 import java.util.*; 5 import org.apache.log4j.Category; 6 7 11 public class TKLib 12 { 13 private static final Category CAT = Category.getInstance(TKLib.class); 14 15 18 public TKLib(){ 19 } 20 21 28 public static String slurpfile(File file){ 29 try{ 30 FileInputStream f = new FileInputStream( file); 31 DataInputStream d = new DataInputStream(f); 32 33 34 byte out[] = new byte[(int) file.length()]; 35 d.readFully(out); 36 String txt = new String (out, 0); 37 38 d.close(); 39 f.close(); 40 41 return(txt); 42 43 } 44 catch(IOException e) 45 { 46 CAT.error("slurpFile: " + e.getMessage() + " in " + file.getAbsolutePath(), e); 47 return("0"); 49 } 50 51 } 52 53 54 60 public static String slurpfile(String filename){ 61 File file = new File( filename ); 62 return slurpfile( file ); 63 } 64 65 66 73 public static void spitfile(File file, String data){ 74 try 75 { 76 FileOutputStream f = new FileOutputStream( file); 77 DataOutputStream d = new DataOutputStream(f); 78 79 d.writeBytes(data); 80 d.close(); 81 f.close(); 82 } 83 catch(IOException e) 84 { 85 CAT.error("spitfile", e); 86 } 87 88 } 89 90 91 97 public static void spitfile(String filename, String data){ 98 File file = new File( filename ); 99 spitfile( file, data ); 100 } 101 102 107 public static final void qsort( Object [] array ) 108 { 109 if( array.length > 1 ) doSort( array, 0, array.length-1 ); 110 } 111 112 119 private static final void doSort( Object [] array, int l, int r ) 120 { 121 int i = l; 122 int j = r; 123 Object x = array[ (l+r) / 2 ]; 124 boolean isSortable = x instanceof TKSortable; 125 if( isSortable ) { 126 TKSortable sx = (TKSortable) x; 127 String xs = x.toString(); 128 do { 129 while( x != array[i] && ( array[i] instanceof TKSortable 130 ? ( ((TKSortable)array[i]).cmp( sx ) < 0 ) 131 : ( array[i].toString().compareTo( xs ) < 0 ) 132 ) ) { 133 i++; 134 } 135 while( x != array[j] && ( array[j] instanceof TKSortable 136 ? ( sx.cmp( ((TKSortable)array[j]) ) < 0 ) 137 : ( xs.compareTo( array[j].toString() ) < 0 ) 138 ) ) { 139 j--; 140 } 141 if( i <= j ) { 142 Object w = array[i]; 143 array[i] = array[j]; 144 array[j] = w; 145 i++; 146 j--; 147 } 148 } while( i <= j ); 149 } 150 else { 151 String xs = x.toString(); 152 do { 153 while( x != array[i] && array[i].toString().compareTo( xs ) < 0 ) i++; 154 while( x != array[j] && xs.compareTo( array[j].toString() ) < 0 ) j--; 155 if( i<= j ) { 156 Object w = array[i]; 157 array[i] = array[j]; 158 array[j] = w; 159 i++; 160 j--; 161 } 162 } while( i <= j ); 163 } 164 if( l < j ) doSort( array, l, j ); 165 if( i < r ) doSort( array, i, r ); 166 } 167 168 169 174 public static final void qrsort( Object [] array ) 175 { 176 if( array.length > 1 ) doReverseSort( array, 0, array.length-1 ); 177 } 178 179 186 private static final void doReverseSort( Object [] array, int l, int r ) 187 { 188 int i = l; 189 int j = r; 190 Object x = array[ (l+r) / 2 ]; 191 boolean isSortable = x instanceof TKSortable; 192 if( isSortable ) { 193 TKSortable sx = (TKSortable) x; 194 String xs = x.toString(); 195 do { 196 while( x != array[i] && ( array[i] instanceof TKSortable 197 ? ( ((TKSortable)array[i]).cmp( sx ) > 0 ) 198 : ( array[i].toString().compareTo( xs ) > 0 ) 199 ) ) { 200 i++; 201 } 202 while( x != array[j] && ( array[j] instanceof TKSortable 203 ? ( sx.cmp( ((TKSortable)array[j]) ) > 0 ) 204 : ( xs.compareTo( array[j].toString() ) > 0 ) 205 ) ) { 206 j--; 207 } 208 if( i <= j ) { 209 Object w = array[i]; 210 array[i] = array[j]; 211 array[j] = w; 212 i++; 213 j--; 214 } 215 } while( i <= j ); 216 } 217 else { 218 String xs = x.toString(); 219 do { 220 while( x != array[i] && array[i].toString().compareTo( xs ) > 0 ) i++; 221 while( x != array[j] && xs.compareTo( array[j].toString() ) > 0 ) j--; 222 if( i<= j ) { 223 Object w = array[i]; 224 array[i] = array[j]; 225 array[j] = w; 226 i++; 227 j--; 228 } 229 } while( i <= j ); 230 } 231 if( l < j ) doReverseSort( array, l, j ); 232 if( i < r ) doReverseSort( array, i, r ); 233 } 234 235 242 public static String fillUp( String src, int len, char c ) 243 { 244 return fillUp( src, len, c, false ); 245 } 246 247 258 public static String fillUp( String val, int len, char c, boolean fillLeft ) 259 { 260 int missing = len - val.length(); 261 if( missing <= 0 ) return val; 262 StringBuffer newVal = new StringBuffer ( val ); 263 while( missing > 0 ) { 264 if( fillLeft ) { 265 newVal.insert( 0, c ); 266 } 267 else { 268 newVal.append( c ); 269 } 270 missing--; 271 } 272 return newVal.toString(); 273 } 274 275 281 public static String fillInt( String src, int len ) 282 { 283 return fillUp( src, len, '0', true ); 284 } 285 286 292 public static String fillString( String src, int len ) 293 { 294 return fillUp( src, len, ' ', false ); 295 } 296 297 301 public static String [] listConcat( String list1[], String list2[]){ 302 303 if (list1 == null){ 304 return(list2); 305 } 306 if (list2 == null){ 307 return(list1); 308 } 309 310 String resultList[] = new String [list1.length + list2.length]; 311 int i; 312 for(i=0;i<list1.length;i++){ 313 resultList[i] = list1[i]; 314 } 315 for(i=0;i<list2.length;i++){ 316 resultList[list1.length +i] = list2[i]; 317 } 318 return(resultList); 319 } 320 321 325 public static String [] listConcat( String list1[], String list2){ 326 327 if (list1 == null){ 328 String rList[] = new String [1]; 329 rList[0] = list2; 330 return(rList); 331 } 332 333 334 String resultList[] = new String [list1.length + 1]; 335 int i; 336 for(i=0;i<list1.length;i++){ 337 resultList[i] = list1[i]; 338 } 339 resultList[list1.length] = list2; 340 341 return(resultList); 342 } 343 344 345 349 public static String [] listConcat( String list1, String list2[]){ 350 351 if (list2 == null){ 352 String rList[] = new String [1]; 353 rList[0] = list1; 354 return(rList); 355 } 356 357 358 359 String resultList[] = new String [list2.length + 1]; 360 int i; 361 362 resultList[0] = list1; 363 for(i=1;i<=list2.length;i++){ 364 resultList[i] = list2[i]; 365 } 366 return(resultList); 367 } 368 369 373 public static String [] listConcat( String list1, String list2){ 374 375 String resultList[] = new String [2]; 376 resultList[0] = list1; 377 resultList[1] = list2; 378 379 return(resultList); 380 } 381 382 383 384 389 public static Hashtable hashConcat( Hashtable hash1 , Hashtable hash2){ 390 391 if (hash1 == null){ 392 return (hash2); 393 } 394 if (hash2 == null){ 395 return (hash1); 396 } 397 398 Enumeration keys = hash2.keys(); 399 Enumeration vals = hash2.elements(); 400 401 while(keys.hasMoreElements()){ 402 Object val, key; 403 404 val = vals.nextElement(); 405 key = keys.nextElement(); 406 407 if (val instanceof Hashtable){ 408 if (hash1.get(key) instanceof Hashtable){ 409 hash1.put(key, hashConcat(((Hashtable)hash1.get(key)), (Hashtable) val)); 410 }else{ 411 hash1.put(key, val); 412 } 413 }else{ 414 if (val instanceof String []){ 415 if (hash1.get(key) instanceof String []){ 416 hash1.put(key, listConcat(((String []) hash1.get(key)), (String []) val)); 417 }else{ 418 if (hash1.get(key) instanceof String ){ 419 hash1.put(key, listConcat(((String ) hash1.get(key)), (String []) val)); 420 }else{ 421 hash1.put(key, val); 422 } 423 } 424 }else{ 425 if (hash1.get(key) instanceof String []){ 426 hash1.put(key, listConcat(((String []) hash1.get(key)), (String ) val)); 427 }else{ 428 if (hash1.get(key) instanceof String ){ 429 hash1.put(key, listConcat(((String ) hash1.get(key)), (String ) val)); 430 }else{ 431 hash1.put(key, val); 432 } 433 } 434 } 435 } 436 } 437 438 return(hash1); 439 } 440 441 444 public static void printHash( Hashtable hash) 445 { 446 Enumeration keys = hash.keys(); 447 Enumeration vals = hash.elements(); 448 449 Object val; 450 451 while(keys.hasMoreElements()){ 452 val = vals.nextElement(); 453 if (val instanceof String []){ 454 int i; 455 CAT.debug(keys.nextElement()+ "= List:"); 456 for(i=0;i<((String []) val).length;i++){ 457 CAT.debug(((String []) val)[i]+","); 458 } 459 }else{ 460 CAT.debug(keys.nextElement() + "=" + val); 461 } 462 } 463 464 } 465 466 467 public static String [] split (String txt, String seperator){ 469 470 StringTokenizer st = new StringTokenizer(txt, seperator); 471 int number = st.countTokens(); 472 String [] list = new String [number]; 473 int i; 474 475 try { 476 for(i=0;i<number;i++){ 477 list[i]= st.nextToken(); 478 } 479 }catch (Exception e){ 480 CAT.error("checkKeys: Wrong number of Tokens in String", e); 481 } 482 483 return(list); 484 } 485 486 public static String getPackageName (Object obj) { 487 488 String pathName = obj.getClass().getName(); 489 int i = pathName.lastIndexOf("."); 490 return i < 0 ? null : pathName.substring (0,i); 491 } 492 493 497 public static String getPackageName (Class cl) { 498 499 String pathName = cl.getName(); 500 int i = pathName.lastIndexOf("."); 501 return i < 0 ? null : pathName.substring (0,i); 502 } 503 504 public static String getClassName (Object obj) { 505 506 String pathName = obj.getClass().getName(); 507 int i = pathName.lastIndexOf("."); 508 return i < 0 ? null : pathName.substring (i+1); 509 } 510 511 515 public static String getClassName (Class cl) { 516 517 String pathName = cl.getName(); 518 int i = pathName.lastIndexOf("."); 519 return i < 0 ? null : pathName.substring (i+1); 520 } 521 } 522 | Popular Tags |