1 | import org.apache.tools.ant.*;
|
---|
2 | import org.apache.tools.ant.taskdefs.*;
|
---|
3 | import 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 | */
|
---|
12 | public 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 | GetFreePath gfp = new GetFreePath();
|
---|
22 | gfp.setPath( new File(args[0]) );
|
---|
23 | gfp.setProperty( args[1] );
|
---|
24 | gfp.execute();
|
---|
25 | }
|
---|
26 |
|
---|
27 | public void execute() {
|
---|
28 |
|
---|
29 | //check both attributes set
|
---|
30 | if ( path == null ) {
|
---|
31 | throw new BuildException( "Error - No path specified !!" );
|
---|
32 | }
|
---|
33 |
|
---|
34 | if ( property == null ) {
|
---|
35 | throw new BuildException( "Error - No property specified !!" );
|
---|
36 | }
|
---|
37 |
|
---|
38 | //find a free path
|
---|
39 | File returnPath = new File(path.getPath());
|
---|
40 | for ( int i=2; returnPath.exists(); i++ ) {
|
---|
41 | returnPath = new File(path.getPath() + "(" + i + ")" );
|
---|
42 | }
|
---|
43 |
|
---|
44 | //set the found path in the project
|
---|
45 | Project pr = getProject();
|
---|
46 | if ( pr != null && pr.getProperty(property) == null ) {
|
---|
47 | pr.setProperty( property, returnPath.getPath() );
|
---|
48 | }
|
---|
49 | System.out.println(returnPath.getPath());
|
---|
50 |
|
---|
51 |
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | public void setPath(File path) {
|
---|
56 | this.path = path;
|
---|
57 | }
|
---|
58 |
|
---|
59 | public void setProperty(String property) {
|
---|
60 | this.property = property;
|
---|
61 | }
|
---|
62 |
|
---|
63 | }
|
---|