source: release-kits/shared/ant-tasks/orans/GetFreePath.java@ 16914

Last change on this file since 16914 was 16914, checked in by oranfry, 16 years ago

a task to find a free place to put files

File size: 1.5 KB
Line 
1import org.apache.tools.ant.*;
2import org.apache.tools.ant.taskdefs.*;
3import java.io.File;
4
5/**
6 * Returns a path, based on the given path, which does not exist on the filesystem
7 * If the given path doesn't exist on the system, just returns the path
8 * If the given path does exist, return the path appended with "(n)" where
9 * where n is the lowest possible integer greater than 1 which would result in a
10 * path that does not exist on the file system.
11 */
12public class GetFreePath extends Task {
13
14 private File path = null;
15 private String property = null;
16
17 /**
18 * for testing
19 */
20 public static void main( String[] args ) {
21
22 GetFreePath gfp = new GetFreePath();
23 gfp.setPath( new File(args[0]) );
24 gfp.setProperty( args[1] );
25 gfp.execute();
26
27 }
28
29 public void execute() {
30
31 //check both attributes set
32 if ( path == null ) {
33 throw new BuildException( "Error - No path specified !!" );
34 }
35
36 if ( property == null ) {
37 throw new BuildException( "Error - No property specified !!" );
38 }
39
40 //find a free path
41 File returnPath = new File(path.getPath());
42 for ( int i=2; returnPath.exists(); i++ ) {
43 returnPath = new File(path.getPath() + "(" + i + ")" );
44 }
45
46 //set the found path in the project
47 Project pr = getProject();
48 if ( pr != null ) {
49 pr.setProperty( "property", returnPath.getPath() );
50 }
51 System.out.println(returnPath.getPath());
52
53
54 }
55
56
57 public void setPath(File path) {
58 this.path = path;
59 }
60
61 public void setProperty(String property) {
62 this.property = property;
63 }
64
65}
Note: See TracBrowser for help on using the repository browser.