1 20 21 package com.methodhead.res; 22 23 import java.io.File ; 24 import java.util.Map ; 25 import java.util.HashMap ; 26 import java.util.Arrays ; 27 import java.util.Comparator ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.ArrayList ; 31 import org.apache.commons.lang.StringUtils; 32 import java.io.IOException ; 33 import org.apache.commons.io.FileUtils; 34 import org.apache.commons.io.IOUtils; 35 import org.apache.commons.lang.exception.ExceptionUtils; 36 import java.io.InputStream ; 37 import java.io.FileOutputStream ; 38 39 42 public class FileManager { 43 44 46 48 52 public static final String VALIDATE_INVALIDDESTPATH = "res.invaliddestpath"; 53 54 58 public static final String VALIDATE_SUBDIROFSELF = "res.subdirofself"; 59 60 64 public static final String VALIDATE_CANTOVERWRITE = "res.cantoverwrite"; 65 66 69 public static final String VALIDATE_INVALIDDESTNAME = "res.invaliddestname"; 70 71 73 75 80 protected File getFileForPath( 81 String path ) 82 throws 83 ResException { 84 85 if ( !ResUtils.isValidPath( path ) ) 89 throw new ResException( "Path is invalid \"" + path + "\"" ); 90 91 String [] parts = path.split( "/", 2 ); 95 96 String rootPart = parts[ 0 ]; 97 String pathPart = ""; 98 99 if ( parts.length > 1 ) 100 pathPart = parts[ 1 ]; 101 102 Directory dir = ( Directory )directories_.get( rootPart ); 103 104 if ( dir == null ) 105 return null; 106 107 File file = new File ( dir.getFile(), pathPart ); 111 112 if ( !file.exists() || !file.isDirectory() ) 113 return null; 114 115 return file; 116 } 117 118 123 public void addDirectory( 124 String name, 125 File dir ) 126 throws 127 ResException { 128 129 if ( !dir.exists() || !dir.isDirectory() ) 130 throw new ResException( 131 dir.getName() + " does not exist or is not a directory." ); 132 133 if ( directories_.containsKey( name ) ) 134 throw new ResException( 135 "\"" + name + "\" has already been added." ); 136 137 Directory d = new Directory(); 138 d.setName( name ); 139 d.setFile( dir ); 140 141 directories_.put( name, d ); 142 } 143 144 148 public Directory[] getDirectories() { 149 Directory[] directories = new Directory[ directories_.keySet().size() ]; 150 151 int i = 0; 155 for ( Iterator iter = directories_.keySet().iterator(); iter.hasNext(); ) { 156 directories[ i++ ] = ( Directory )directories_.get( iter.next() ); 157 } 158 159 Arrays.sort( directories, new Comparator () { 163 public int compare( Object o1, Object o2 ) { 164 return 165 ( ( Directory )o1 ).getName().compareToIgnoreCase( 166 ( ( Directory )o2 ).getName() ); 167 } 168 } ); 169 170 return directories; 171 } 172 173 177 public File getFile( 178 String path, 179 String name ) 180 throws 181 ResException { 182 183 File dir = getFileForPath( path ); 187 188 if ( dir == null ) 189 return null; 190 191 File file = new File ( dir, name ); 195 196 if ( !file.exists() ) 197 return null; 198 199 return file; 200 } 201 202 206 public File [] getFiles( 207 String path ) 208 throws 209 ResException { 210 211 File dir = getFileForPath( path ); 215 216 if ( dir == null ) 217 return null; 218 219 File [] files = dir.listFiles(); 223 224 if ( files == null ) 225 throw new ResException( "Could list files for path \"" + path + "\"." ); 226 227 Arrays.sort( files, new Comparator () { 231 public int compare( Object o1, Object o2 ) { 232 return 233 ( ( File )o1 ).getName().compareToIgnoreCase( 234 ( ( File )o2 ).getName() ); 235 } 236 } ); 237 238 return files; 239 } 240 241 245 protected boolean isDestSubdir( 246 String srcPath, 247 String [] srcFiles, 248 String destPath ) { 249 250 for ( int i = 0; i < srcFiles.length; i++ ) { 251 String filePath = srcPath + "/" + srcFiles[ i ]; 252 253 if ( ResUtils.isPathDescendent( 254 ResUtils.cleanPath( filePath ), ResUtils.cleanPath( destPath ) ) ) 255 return true; 256 } 257 258 return false; 259 } 260 261 266 protected boolean canOverwrite( 267 File src, 268 File dest ) { 269 270 return canOverwrite( dest, src.isDirectory() ); 271 272 286 } 287 288 295 protected boolean canOverwrite( 296 File dest, 297 boolean isDir ) { 298 299 if ( dest != null ) { 300 if ( dest.isDirectory() ) { 301 return false; 302 } 303 else { 304 if ( isDir ) { 305 return false; 306 } 307 } 308 } 309 310 return true; 311 } 312 313 323 public String validateMove( 324 String srcPath, 325 String [] srcFiles, 326 String destPath, 327 String destFile ) { 328 329 if ( StringUtils.isBlank( destPath ) ) 333 return VALIDATE_INVALIDDESTPATH; 334 335 if ( !ResUtils.isValidPath( destPath ) ) 339 return VALIDATE_INVALIDDESTPATH; 340 341 if ( getFileForPath( destPath ) == null ) 345 return VALIDATE_INVALIDDESTPATH; 346 347 if ( isDestSubdir( srcPath, srcFiles, destPath ) ) 351 return VALIDATE_SUBDIROFSELF; 352 353 if ( srcFiles.length == 1 ) { 354 355 if ( StringUtils.isBlank( destFile ) ) 359 return VALIDATE_INVALIDDESTNAME; 360 361 if ( !ResUtils.isValidFileName( destFile ) ) 365 return VALIDATE_INVALIDDESTNAME; 366 367 if ( !canOverwrite( 371 getFile( srcPath, srcFiles[ 0 ] ), 372 getFile( destPath, destFile ) ) ) 373 return VALIDATE_CANTOVERWRITE; 374 } 375 else { 376 377 for ( int i = 0; i < srcFiles.length; i++ ) { 381 if ( !canOverwrite( 382 getFile( srcPath, srcFiles[ i ] ), 383 getFile( destPath, srcFiles[ i ] ) ) ) 384 return VALIDATE_CANTOVERWRITE; 385 } 386 } 387 388 return null; 389 } 390 391 401 public String validateCreate( 402 String srcPath, 403 String srcFile, 404 boolean isDir ) { 405 406 File dest = getFile( srcPath, srcFile ); 407 408 if ( ( dest != null ) && ( !canOverwrite( dest, isDir ) ) ) 412 return VALIDATE_CANTOVERWRITE; 413 414 return null; 415 } 416 417 423 public String [] findOverwriteFiles( 424 String srcPath, 425 String [] srcFiles, 426 String destPath, 427 String destFile ) { 428 429 if ( srcFiles.length == 1 ) { 430 if ( getFile( destPath, destFile ) != null ) 431 return new String [] { destFile }; 432 } 433 else { 434 List l = new ArrayList (); 435 for ( int i = 0; i < srcFiles.length; i++ ) { 436 if ( getFile( destPath, srcFiles[ i ] ) != null ) 437 l.add( srcFiles[ i ] ); 438 } 439 440 if ( l.size() > 0 ) { 441 String [] fileNames = new String [ l.size() ]; 442 int i = 0; 443 for ( Iterator iter = l.iterator(); iter.hasNext(); ) 444 fileNames[ i++ ] = ( String )iter.next(); 445 446 return fileNames; 447 } 448 } 449 450 return null; 451 } 452 453 461 protected static void copyFile( 462 File from, 463 File to ) 464 throws 465 ResException { 466 467 try { 468 if ( from.isDirectory() ) { 469 if ( to.exists() ) { 470 if ( !to.isDirectory() ) 471 throw new ResException( 472 "Can't copy directory over non-directory: " + 473 to.getAbsolutePath() ); 474 } 475 else { 476 to.mkdir(); 477 } 478 479 File [] files = from.listFiles(); 483 484 for ( int i = 0; i < files.length; i++ ) 485 copyFile( files[ i ], new File ( to, files[ i ].getName() ) ); 486 } 487 else { 488 if ( to.exists() && to.isDirectory() ) 489 throw new ResException( 490 "Can't copy non-directory over directory: " + 491 to.getAbsolutePath() ); 492 493 FileUtils.copyFile( from, to ); 494 } 495 } 496 catch ( IOException e ) { 497 throw new ResException( 498 "Unexpected IOException copying \"" + from + "\" to \"" + to + 499 "\": " + ExceptionUtils.getStackTrace( e ) ); 500 } 501 } 502 503 508 public void move( 509 String srcPath, 510 String [] srcFiles, 511 String destPath, 512 String destFile ) { 513 514 if ( srcFiles.length == 1 ) { 515 File src = getFile( srcPath, srcFiles[ 0 ] ); 516 File dest = new File ( getFileForPath( destPath ), destFile ); 517 src.renameTo( dest ); 518 } 519 else { 520 for ( int i = 0; i < srcFiles.length; i++ ) { 521 File src = getFile( srcPath, srcFiles[ i ] ); 522 File dest = new File ( getFileForPath( destPath ), srcFiles[ i ] ); 523 src.renameTo( dest ); 524 } 525 } 526 } 527 528 534 public void copy( 535 String srcPath, 536 String [] srcFiles, 537 String destPath, 538 String destFile ) { 539 540 if ( srcFiles.length == 1 ) { 541 File src = getFile( srcPath, srcFiles[ 0 ] ); 542 File dest = new File ( getFileForPath( destPath ), destFile ); 543 copyFile( src, dest ); 544 } 545 else { 546 for ( int i = 0; i < srcFiles.length; i++ ) { 547 File src = getFile( srcPath, srcFiles[ i ] ); 548 File dest = new File ( getFileForPath( destPath ), srcFiles[ i ] ); 549 copyFile( src, dest ); 550 } 551 } 552 } 553 554 558 public void delete( 559 String srcPath, 560 String [] srcFiles ) { 561 562 try { 563 for ( int i = 0; i < srcFiles.length; i++ ) { 567 if ( StringUtils.isBlank( srcFiles[ i ] ) ) 568 throw new ResException( "Can't delete a file with a blank name." ); 569 570 File f = getFile( srcPath, srcFiles[ i ] ); 571 if ( f == null ) 572 throw new ResException( 573 "\"" + srcPath + "/" + srcFiles[ i ] + "\" doesn't exist" ); 574 } 575 576 for ( int i = 0; i < srcFiles.length; i++ ) { 580 File f = getFile( srcPath, srcFiles[ i ] ); 581 if ( f.isDirectory() ) 582 FileUtils.deleteDirectory( f ); 583 else 584 f.delete(); 585 } 586 } 587 catch ( IOException e ) { 588 throw new ResException( 589 "Unexpected exception: " + ExceptionUtils.getStackTrace( e ) ); 590 } 591 } 592 593 598 public File getNewFile( 599 String path, 600 String name ) 601 throws 602 ResException { 603 604 if ( getFile( path, name ) != null ) 608 throw new ResException( "File exists \"" + path + "/" + name + "\"" ); 609 610 File dir = getFileForPath( path ); 614 615 if ( dir == null ) 616 throw new ResException( "Invalid path \"" + path + "\"" ); 617 618 return new File ( dir, name ); 622 } 623 624 630 public void create( 631 String path, 632 String file, 633 boolean isDir ) { 634 635 try { 636 File dir = getFileForPath( path ); 640 641 if ( dir == null ) 642 throw new ResException( "Invalid path \"" + path + "\"" ); 643 644 File f = new File ( dir, file ); 648 649 if ( f.exists() ) { 650 if ( f.isDirectory() ) 651 throw new ResException( "Can't overwrite directory." ); 652 else { 653 f.delete(); 654 } 655 } 656 657 if ( isDir ) 658 f.mkdir(); 659 else 660 f.createNewFile(); 661 } 662 catch ( IOException e ) { 663 throw new ResException( 664 "Unexpected IOException: " + ExceptionUtils.getStackTrace( e ) ); 665 } 666 } 667 668 673 public void create( 674 String path, 675 String file, 676 InputStream in ) { 677 678 try { 679 File dir = getFileForPath( path ); 683 684 if ( dir == null ) 685 throw new ResException( "Invalid path \"" + path + "\"" ); 686 687 File f = new File ( dir, file ); 691 692 if ( f.exists() ) { 693 if ( f.isDirectory() ) 694 throw new ResException( "Can't overwrite directory." ); 695 else { 696 f.delete(); 697 } 698 } 699 700 FileOutputStream out = new FileOutputStream ( f ); 701 IOUtils.copy( in, out ); 702 out.close(); 703 } 704 catch ( IOException e ) { 705 throw new ResException( 706 "Unexpected IOException: " + ExceptionUtils.getStackTrace( e ) ); 707 } 708 } 709 710 712 714 protected Map directories_ = new HashMap (); 715 } 716
| Popular Tags
|