1 | /*
|
---|
2 | * GetFreePath.java
|
---|
3 | * A task to find a free folder to put something in
|
---|
4 | *
|
---|
5 | * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
|
---|
6 | *
|
---|
7 | * This program is free software; you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation; either version 2 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This program is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with this program; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
20 | */
|
---|
21 | package org.greenstone.anttasks;
|
---|
22 |
|
---|
23 | import org.apache.tools.ant.*;
|
---|
24 | import org.apache.tools.ant.taskdefs.*;
|
---|
25 | import java.io.File;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Returns a path, based on the given path, which does not exist on the filesystem
|
---|
29 | * If the given path doesn't exist on the system, just returns the path
|
---|
30 | * If the given path does exist, return the path appended with "(n)" where
|
---|
31 | * where n is the lowest possible integer greater than 1 which would result in a
|
---|
32 | * path that does not exist on the file system.
|
---|
33 | */
|
---|
34 | public class GetFreePath extends Task {
|
---|
35 |
|
---|
36 | private File path = null;
|
---|
37 | private String property = null;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * for testing
|
---|
41 | */
|
---|
42 | public static void main( String[] args ) {
|
---|
43 | GetFreePath gfp = new GetFreePath();
|
---|
44 | gfp.setPath( new File(args[0]) );
|
---|
45 | gfp.setProperty( args[1] );
|
---|
46 | gfp.execute();
|
---|
47 | }
|
---|
48 |
|
---|
49 | public void execute() {
|
---|
50 |
|
---|
51 | //check both attributes set
|
---|
52 | if ( path == null ) {
|
---|
53 | throw new BuildException( "Error - No path specified !!" );
|
---|
54 | }
|
---|
55 |
|
---|
56 | if ( property == null ) {
|
---|
57 | throw new BuildException( "Error - No property specified !!" );
|
---|
58 | }
|
---|
59 |
|
---|
60 | //find a free path
|
---|
61 | File returnPath = new File(path.getPath());
|
---|
62 | for ( int i=2; returnPath.exists(); i++ ) {
|
---|
63 | returnPath = new File(path.getPath() + "(" + i + ")" );
|
---|
64 | }
|
---|
65 |
|
---|
66 | //set the found path in the project
|
---|
67 | Project pr = getProject();
|
---|
68 | if ( pr != null && pr.getProperty(property) == null ) {
|
---|
69 | pr.setProperty( property, returnPath.getPath() );
|
---|
70 | }
|
---|
71 | System.out.println(returnPath.getPath());
|
---|
72 |
|
---|
73 |
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | public void setPath(File path) {
|
---|
78 | this.path = path;
|
---|
79 | }
|
---|
80 |
|
---|
81 | public void setProperty(String property) {
|
---|
82 | this.property = property;
|
---|
83 | }
|
---|
84 |
|
---|
85 | }
|
---|